整体来说链表题都可以通过多个指针来解,或者借助其他的数据结构
这边采用双指针的思路
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head==null||head.next==null){
return head;
}
ListNode start=head;
ListNode estart=head.next;
while(start!=null&&start.next!=null){
if(start.val==estart.val){
while(estart!=null){
if(estart.val==start.val){
estart=estart.next;
}else{
break;
}
}
start.next=estart;
if(estart==null){
return head;
}else{
start=estart;
}
}else{
start=start.next;
estart=start.next;
}
}
return head;
}
}
京公网安备 11010502036488号