ice rabbit programming

[LeetCode] Remove Duplicates From Sorted List 본문

PS/LeetCode

[LeetCode] Remove Duplicates From Sorted List

판교토끼 2020. 4. 15. 16:54

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

[

Remove Duplicates from Sorted 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

](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)

이 문제와 비슷한데, 여기서는 배열이 아닌 List이다. value가 같으면 연결을 끊고 그 다음을 연결했다. null 검사를 통해 루프를 탈출.

// C#
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode DeleteDuplicates(ListNode head) {
        ListNode cmpTemp = head;
        if(cmpTemp==null)
            return head;
        while(cmpTemp.next!=null) {
            if(cmpTemp.val==cmpTemp.next.val) {
                cmpTemp.next = cmpTemp.next.next;
                continue;
            }
            cmpTemp = cmpTemp.next;
        }
        return head;
    }
}

'PS > LeetCode' 카테고리의 다른 글

[LeetCode] Two Sum II - Input array is sorted  (0) 2020.04.18
[LeetCode] Linked List Cycle  (0) 2020.04.16
[LeetCode] Climbing Stairs  (0) 2020.04.15
[LeetCode] Maximum Subarray  (0) 2020.04.15
[LeetCode] Valid Parenthese  (0) 2020.04.15