Remove Duplicates from Sorted List II
2018-02-11 15:14
169 查看
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,
Given
Given
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode result = new ListNode(0);
result.next = head;
ListNode pre = result;
ListNode cur = pre.next;
ListNode next = cur.next;
boolean flag = false;
while(true){
if(next == null)
break;
if(cur.val == next.val){
flag = true;
next = next.next;
}
else{
if(flag){
pre.next = next;
flag = false;
}
else{
pre = cur;
}
cur = next;
next = next.next;
}
}
if(flag){
pre.next = next;
}
return result.next;
}
}如果有重复元素的话将重复指示器设为true(当判定不等时,之前有没有重复元素对于链表的操作不同)然后next指向下一个元素,直到不重复时,判定重复指示器是否为true,如果是,代表当前元素之前是重复元素,则pre.next指向next(删除中间重复元)next赋值给cur,next下移,循环继续。最后如果退出循环时指示器为true,代表末尾有若干重复元素,则将pre.next设置为null(即next)最后返回虚拟表头的next即可(注意不要返回head),因为head表与虚拟表是两个表,我们并没有对head表进行操作。
Given
1->2->3->3->4->4->5, return
1->2->5.
Given
1->1->1->2->3, return
2->3.题目大意:删除全部重复元,只保留不重复的元素题目思路:为了节省工作量最好设置一个虚拟链表头,他的next指向head,这样的话可以避免从head就开始删除时工作量较大的问题,首先可以维护三个节点变量用来进行增删节点的操作。下面是代码:/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode result = new ListNode(0);
result.next = head;
ListNode pre = result;
ListNode cur = pre.next;
ListNode next = cur.next;
boolean flag = false;
while(true){
if(next == null)
break;
if(cur.val == next.val){
flag = true;
next = next.next;
}
else{
if(flag){
pre.next = next;
flag = false;
}
else{
pre = cur;
}
cur = next;
next = next.next;
}
}
if(flag){
pre.next = next;
}
return result.next;
}
}如果有重复元素的话将重复指示器设为true(当判定不等时,之前有没有重复元素对于链表的操作不同)然后next指向下一个元素,直到不重复时,判定重复指示器是否为true,如果是,代表当前元素之前是重复元素,则pre.next指向next(删除中间重复元)next赋值给cur,next下移,循环继续。最后如果退出循环时指示器为true,代表末尾有若干重复元素,则将pre.next设置为null(即next)最后返回虚拟表头的next即可(注意不要返回head),因为head表与虚拟表是两个表,我们并没有对head表进行操作。
相关文章推荐
- [LeetCode] Remove Duplicate from Sorted Linkded List II
- Remove Duplicates from Sorted List II--LeetCode
- Remove Duplicates from Sorted List II (Java)
- leetcode先刷_Remove Duplicates from Sorted List II
- [LeetCode]82 Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- 闲话链表(四) leetcode之Remove Duplicates from Sorted List I、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 ListII 有序链表去重II D3F3
- [leetcode]Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- 【Leetcode】Remove Duplicates from Sorted List II (Removing)
- leetcode 82:Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- LeetCdoe Remove Duplicates from Sorted List II移掉重复链表中的元素
- LeetCode - Remove Duplicates from Sorted List II