Two pointers, and update both if not the same, otherwise update the second and adjust the first's next pointer.
/**
* 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){
return head;
}
ListNode curNode = head;
ListNode node = curNode.next;
while(node != null){
if(node.val == curNode.val){
ListNode temp = node;
node = node.next;
curNode.next = node;
temp.next = null;
}else{
curNode = node;
node = node.next;
}
}
return head;
}
}
没有评论:
发表评论