题解 | 删除有序链表中重复的元素-I
删除有序链表中重复的元素-I
https://www.nowcoder.com/practice/c087914fae584da886a0091e877f2c79
import java.util.*;
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
while (head != null) {
if (pre.val == head.val) {
//如果相同,pre停下,删除head,head走一步再判断
pre.next = head.next;
head = head.next;
} else {
//如果不同,双个指针都走
pre = pre.next;
head = head.next;
}
}
return dummy.next;
}
}
查看13道真题和解析