1. 程式人生 > >[leetcode] 83. Remove Duplicates from Sorted List 解題報告

[leetcode] 83. Remove Duplicates from Sorted List 解題報告

ted leetcode index turn pre div logs 備份 cat

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

備份頭指針,分3種情況判斷即可

public ListNode deleteDuplicates(ListNode head) {
        if (head==null || head.next==null
) return head; ListNode index =head; while (index.next!=null){ if (index.next.val==index.val && index.next.next!=null){ index.next=index.next.next; }else if (index.next.val==index.val && index.next.next==null){ index.next
=null; }else { index=index.next; } } return head; }

[leetcode] 83. Remove Duplicates from Sorted List 解題報告