[Linked List, Easy] Reverse Linked List

송재호·2025년 3월 14일
0

https://leetcode.com/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=leetcode-75

현재 노드의 next와 prev를 바꿔주는 것이 핵심
최종적으로 뒤집힌 포인터를 바라보게 되므로 prev를 리턴해야 역순 탐색이 가능해 정답처리된다.

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;
    }
}
profile
식지 않는 감자

0개의 댓글