/**
* 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 dum = new ListNode(0, head);
//当前结点
ListNode cur = dum;
while (cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
//重复出现的数字
int a = cur.next.val;
//把重复出现的结点全给删除
while (cur.next != null && cur.next.val == a) {
cur.next = cur.next.next;
}
}
else {
cur = cur.next;
}
}
return dum.next;
}
}
删除排序链表中的重复元素II
最新推荐文章于 2022-06-30 14:50:26 发布