标签:
Remove Duplicates from Sorted List II
问题:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
思路:
prepre pre cur 指针的相互作用
我的代码:
public class Solution { public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null) return head; ListNode dummy = new ListNode(-1); dummy.next = head; ListNode prepre = dummy; ListNode pre = head; ListNode cur = head.next; while(cur != null) { if(cur.val == pre.val) { while(cur != null && cur.val == pre.val) { cur = cur.next; } prepre.next = cur; pre = cur; if(cur != null) cur = cur.next; else cur = null; } else { prepre = pre; pre = cur; cur = cur.next; } } return dummy.next; } }
学习之处:
public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) { return null; } ListNode node = head; while (node.next != null) { if (node.val == node.next.val) { node.next = node.next.next; } else { node = node.next; } } return head; } }
Remove Duplicates from Sorted List II
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4338981.html