Remove Duplicates from Sorted List
思路一: 双指针,左指针记录链表最后有效位置,右指针向前扫描,遇到不重复的值,加到左指针后面,双指针依次向前
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode left = head;
ListNode right = head;
while (right != null) {
if (left.val != right.val) {
left = left.next;
left.val = right.val;
}
right = right.next;
}
if (left.next != null) {
left.next = null;
}
return head;
}
标签:head,right,val,next,easy,83,null,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/16789173.html