Home 206. Reverse Linked List
Post
Cancel

206. Reverse Linked List

정리 코드

Recursion Function(재귀함수) : 자기 자신을 재참조, 종료 조건으로 종료 시점을 제어한다.


leetcode : 206. Reverse Linked List 문제


  • 조건
    • The number of nodes in the list is the range [0, 5000].
    • -5000 <= Node.val <= 5000


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * 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) {
    if (head == null || head.next == null) {
      return head;
    }
    ListNode nextNode = head.next;
    head.next = null;
    ListNode current = reverseList(nextNode);
    nextNode.next = head;
    return current;
  }
}
This post is licensed under CC BY 4.0 by the author.