출처 : https://leetcode.com/problems/reverse-linked-list/?envType=study-plan-v2&envId=programming-skills
Given the head
of a singly linked list, reverse the list, and return the reversed list.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode previous = null;
ListNode current = head;
while (current != null) {
ListNode next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}
}
🙈 풀이 참조한 문제
출처 : https://leetcode.com/problems/reverse-linked-list/discuss/2682085/JAVA-0ms-100-Easy-Understanding