알고리즘

Reverse Linked List

e-pd 2020. 8. 26. 15:08

https://leetcode.com/problems/reverse-linked-list/

 

Reverse Linked List - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode next = null;
        ListNode current = head;
        
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }
}
cs

코드는 간단한데 원리를 이해하는게 어렵다.

종이와 펜으로 반복해서 교체되는 원리를 따라가봐야한다.

 

youtu.be/D7y_hoT_YZI