코린이의 소소한 공부노트

[LeetCode/Easy] 83. Remove Duplicates from Sorted List 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 83. Remove Duplicates from Sorted List

무지맘 2023. 7. 17. 18:58

1. Input

1) ListNode head

 

2. Output

1) 정렬된 연결 리스트 head에서 모든 중복을 제외한 결과 리스트를 반환

 

3. Constraint

1) 노드 수의 범위는 [0, 300]이다.

2) -100 <= Node.val <= 100

3) 리스트는 오름차순으로 정렬되어 있다.

 

4. Example

Input: head = [1,1,2] -> Output: [1,2]

Input: head = [1,1,2,3,3] -> Output: [1,2,3]

 

5. Code

/**
 * 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 deleteDuplicates(ListNode head) {
        ListNode current = head;
        while(current!=null){
            if(current.next!=null && current.val == current.next.val){
                if(current.next.next!=null)
                    current.next = current.next.next;
                else
                    current.next = null;
            }
            else        
                current = current.next;
        }
        return head;
    }
}

- 100%, 63%