82. Remove Duplicates from Sorted List II

pre.next设置成下一个数字的第一个 下一个数字进行循环 循环到相同的数字的最后一个 要是pre.next = node1 说明只有unique pre=pre.next, 否则说明不unique pre.next设置成下一个数字的第一个 就是pre.next = node1.next

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/discuss/28335/My-accepted-Java-code?page=1

 

 

 

 

 1 class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
 3         if(head == null || head.next == null) return head;
 4         ListNode node1 = head;
 5         ListNode dummy = new ListNode(0);
 6         dummy.next = head;
 7         ListNode pre = dummy;
 8         while(node1 != null) {
 9             if(node1.next != null && node1.val == node1.next.val){
10                 node1 = node1.next;
11             }else {
12                 if(pre.next == node1) {
13                     pre = pre.next;
14                 }else {
15                     pre.next = node1.next;
16                 }
17                 node1 = node1.next;
18             }
19         }
20         return dummy.next;  
21     }
22 }

 

posted @ 2018-09-16 03:13  jasoncool1  阅读(90)  评论(0编辑  收藏  举报