1. 程式人生 > >[leetcode]83. Remove Duplicates from Sorted List有序鏈表去重

[leetcode]83. Remove Duplicates from Sorted List有序鏈表去重

nbsp IV element pan while mage next ima ret

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

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

題意:

有序鏈表去重

思路:

技術分享圖片

技術分享圖片

代碼:

 1 class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
3 if(head == null) return head; 4 ListNode cur = head; 5 while(cur.next != null){ 6 if(cur.val == cur.next.val){ 7 cur.next = cur.next.next; 8 }else{ 9 cur = cur.next; 10 } 11 } 12 return
head; 13 } 14 }

[leetcode]83. Remove Duplicates from Sorted List有序鏈表去重