CODE 50: Remove Duplicates from Sorted List II
2013-09-29 22:44
155 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
public ListNode deleteDuplicates(ListNode head) { // Start typing your Java solution below // DO NOT write main() function if (null == head) { return null; } ListNode node = new ListNode(0); ListNode next = head.next; int currentVal = head.val; ListNode currentNode = node; boolean isDouble = false; while (null != next) { if (next.val > currentVal) { if (!isDouble) { currentNode.next = new ListNode(currentVal); currentNode = currentNode.next; } isDouble = false; currentVal = next.val; } else if (next.val == currentVal) { isDouble = true; } next = next.next; } if (!isDouble) { currentNode.next = new ListNode(currentVal); } return node.next; }
相关文章推荐
- lintcode:Remove Duplicates from Sorted List II
- [leet code] Remove Duplicates from Sorted List II
- LeetCode_Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II(Python3)
- LeetCode | Remove Duplicates from Sorted List II
- LeetCode2.2.5(Remove Duplicates from Sorted List II)
- Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II [083]
- LeetCode OJ Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- Leetcode Remove Duplicates from Sorted List II
- [C++]LeetCode: 6 Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- LeetCode Solutions : Remove Duplicates from Sorted List I & II
- LeetCode82:Remove Duplicates from Sorted List II
- LeetCode Remove Duplicates from Sorted List II