您的位置:首页 > 其它

Remove Duplicates from Sorted List II

2014-06-15 02:55 274 查看
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(head==null||head.next==null){//其实该if语句块是直接在else里面包含的
return head;
}else{
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode preNode=dummy;
ListNode curNode=head;
while(curNode!=null){
if(curNode.next!=null){
if(curNode.next.val!=curNode.val){
preNode=curNode;
curNode=curNode.next;
}else{
curNode=curNode.next;
while((curNode.next!=null)&&(curNode.next.val==curNode.val)){
curNode=curNode.next;
}
if(curNode==null){
preNode.next=null;
return dummy.next;
}
preNode.next=curNode.next;//接下来两行代码,先写哪一行 效率也是不一样的,可以看下面的优化
curNode=curNode.next;
}
}else{
return dummy.next;
}

}
return dummy.next;

}
}
}


1)边界问题,1 刚开始便是duplicates 2 结尾是重复

2)linkedlist 利用dummy node 可以方便解题

看了看答案,根据第三个答案,又大概优化了一下自己的代码(程序应该是可以更优的,但是现在不想改了,下次看到了,再继续优化):

/**
* 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) {
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode preNode=dummy;
ListNode curNode=head;
while(curNode!=null){
if(curNode.next!=null){
if(curNode.next.val!=curNode.val){
preNode=curNode;
curNode=curNode.next;
}else{
curNode=curNode.next;
while((curNode.next!=null)&&(curNode.next.val==curNode.val)){
curNode=curNode.next;
}
curNode=curNode.next;
preNode.next=curNode;
}
}else{
return dummy.next;
}

}
return dummy.next;

}

}


答案:
/**
* 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) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null || head.next==null) return head;
ListNode dummy=new ListNode(0);
dummy.next=head;
boolean det=false;
ListNode pre=dummy;
ListNode wait=head;
ListNode runner=head.next;
while(runner!=null)
{
if(runner.val==wait.val)
{
det=true;
wait.next=runner.next;
}else
{
if(det)
{
pre.next=runner;
wait=runner;
det=false;
}else
{
pre=wait;
wait=runner;
}
}
runner=runner.next;
}
if(det) pre.next=null;
return dummy.next;
}
}

/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode dumb = new ListNode(Integer.MIN_VALUE);
dumb.next = head;
ListNode curNode = dumb;
boolean hasDup = false;
while(curNode != null && curNode.next != null && curNode.next.next != null) {
if(curNode.next.val == curNode.next.next.val) {
curNode.next = curNode.next.next;
hasDup = true;
} else {
if(!hasDup) {
curNode = curNode.next;
} else {
curNode.next = curNode.next.next;
}
hasDup = false;
}
}
if(hasDup) curNode.next = null;
return dumb.next;
}
}

public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
while(cur.next != null && cur.next.next != null) {
if(cur.next.val == cur.next.next.val) {
ListNode runner = cur.next.next.next;
while(runner != null && runner.val == cur.next.val) {
runner = runner.next;
}
cur.next = runner;
} else {
cur = cur.next;
}
}
return dummy.next;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: