Remove Duplicates from Sorted List II
2014-11-26 19:09
232 查看
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.For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode deleteDuplicates(ListNode head) { if(null == head || null == head.next) //只有1个节点或者空时,直接返回头结点 return head; ListNode temp = new ListNode(0); temp.next = head; head = temp;//加一个头结点,不放内容 ListNode pre = head; ListNode behind = head.next; ListNode front = behind.next; while(null != front){ if(behind.val == front.val){ //如果有相同的,删除掉 while(null != front && behind.val == front.val) front = front.next; if(null == front) pre.next = null; else{ pre.next = front; behind = front; front = front.next; continue; } } if(null != front){ front = front.next; behind = behind.next; pre = pre.next; } } return head.next; } }
相关文章推荐
- Remove Duplicates from Sorted List II (删除排序链表的重复II)
- #113 Remove Duplicates from Sorted List II
- 链表-leetcode 82 Remove Duplicates from Sorted List II
- Leetcode--Remove Duplicates from Sorted List II
- [Leetcode] #82#83 Remove Duplicates from Sorted List I & II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- 【LeetCode】82 - Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List I II
- [LeetCode] Remove Duplicates from Sorted List II
- LeetCode(M) Remove Duplicates from Sorted List II
- 【LeetCode练习题】Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- 【链表】Remove Duplicates from Sorted List II
- leetcode[82]:Remove Duplicates from Sorted List II
- [刷题]Remove Duplicates from Sorted List II
- LeetCode 82: Remove Duplicates from Sorted List II
- Leetcode#82 Remove Duplicates from Sorted List II
- 【LeetCode】Remove Duplicates from Sorted List I && II
- Insertion Sort List && Remove Duplicates from Sorted List II && Intersection of Two Linked Lists