Home 203. Remove Linked List Elements
Post
Cancel

203. Remove Linked List Elements

정리 코드

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


leetcode : 203. Remove Linked List Elements 문제


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


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 removeElements(ListNode head, int val) {
    if (head == null) {
      return null;
    }
    if (head.val == val) {
      return removeElements(head.next, val);
    }
    head.next = removeElements(head.next, val);
    return head;
  }
}
This post is licensed under CC BY 4.0 by the author.