Remove Duplicates from Sorted List

class Solution {
		public ListNode deleteDuplicates(ListNode head) {
			if (head == null) return null; //corner case 
		
			ListNode dummy = new ListNode(0 == head.val ? 1 : 0); //需要val不与head相同
			dummy.next = head;
			ListNode prev = dummy;
			ListNode cur = head;
			S
			ListNode tail = dummy;
			
			while (cur != null && cur.next != null) {
				if (cur.val != cur.next.val && cur.val != prev.val) {
				tail.next = cur;
				tail = tail.next;
				}
				prev = cur;
				cur = cur.next;
			}
			if (prev.val != cur.val) {
				tail.next = cur;
				tail = tail.next;
			}
			tail.next = null;
			return dummy.next;
		}
}

最后更新于

这有帮助吗?