Java for LeetCode 082 Remove Duplicates from Sorted List II
2015-05-19 12:29
465 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given
Given
解题思路:
用指针即可,JAVA实现如下:
For example,
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.
解题思路:
用指针即可,JAVA实现如下:
public ListNode deleteDuplicates(ListNode head) { while (head != null && head.next != null) { ListNode temp = head.next; if (temp.val == head.val) { while (temp.val == head.val) { temp = temp.next; if (temp == null) return null; } head = temp; continue; } else break; } if (head == null || head.next == null) return head; ListNode temp = head.next; ListNode last = head; while (temp != null && temp.next != null) { if (temp.val != temp.next.val) { last.next = temp; temp = temp.next; last=last.next; continue; } int tmp=temp.val; while(temp.val==tmp){ temp=temp.next; if (temp == null){ last.next=null; return head; } } } last.next=temp; return head; }
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- leetcode 082 Remove Duplicates from Sorted List II
- [LeetCode][Java] Remove Duplicates from Sorted List II
- LeetCode082 Remove Duplicates from Sorted List II
- LeetCode – Remove Duplicates from Sorted List II (Java)
- Remove Duplicates from Sorted List II leetcode java
- leetcode JAVA Remove Duplicates from Sorted List II 难度系数3 3.29
- leetcode_c++:链表:Remove Duplicates from Sorted List II(082)
- leetcode解题方案--082--Remove Duplicates from Sorted ListII
- [LeetCode]Remove Duplicates from Sorted List II Java
- 082 Remove Duplicates from Sorted List II [Leetcode]
- [LeetCode][Java] Remove Duplicates from Sorted List II
- LeetCode 082 Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II-- Leetcode 我的java题解
- LeetCode 082 Remove Duplicates from Sorted List II
- LeetCode 82之Remove Duplicates from Sorted List II的Java题解
- Java for LeetCode 083 Remove Duplicates from Sorted List
- [LeetCode]题解(python):082-Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II (Java)
- Remove Duplicates from Sorted List II leetcode java