思路:
// @Title: 删除排序链表中的重复元素 (Remove Duplicates from Sorted List)
// @Author: qisiii
// @Date: 2020-06-01 19:07:13
// @Runtime: 1 ms
// @Memory: 38.2 MB
// @comment:
// @flag:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode anext=head;
while(anext!=null&&anext.next!=null){
if(anext.val==anext.next.val){
anext.next=anext.next.next;
continue;
}
anext=anext.next;
}
return head;
}
}
+++ title = “删除排序链表中的重复元素 (Remove Duplicates from Sorted List)” draft = false +++
思路:两个指针指向前后节点,后指针每轮移动,前指针只有一定不一样了才移动
// @Title: 删除排序链表中的重复元素 (Remove Duplicates from Sorted List)
// @Author: qisiii
// @Date: 2024-01-14 16:23:05
// @Runtime: 0 ms
// @Memory: 43.3 MB
// @comment: 两个指针指向前后节点,后指针每轮移动,前指针只有一定不一样了才移动
// @flag: WHITE
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null){
return head;
}
ListNode pre=head;
ListNode now=head.next;
while(now!=null){
if(pre.val==now.val){
pre.next=now.next;
}else{
pre=now;
}
now=now.next;
}
return head;
}
}