This commit is contained in:
kkunkka
2024-01-15 17:43:14 +08:00
parent cf1c98eac1
commit 98ac64c918
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package com.dota.link._82;
import com.dota.ListNode;
/**
* 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 null;
var t = new ListNode(0, head);
var p = t;
while (p.next != null && p.next.next != null) {
if (p.next.val == p.next.next.val) {
var v = p.next.val;
while (p.next != null && p.next.val == v) {
p.next = p.next.next;
}
} else {
p = p.next;
}
}
return t.next;
}
}

View File

@@ -0,0 +1,23 @@
package com.dota.link._83;
import com.dota.ListNode;
/**
* 83. 删除排序链表中的重复元素
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
var p = head;
while (p.next != null) {
if (p.val == p.next.val) {
p.next = p.next.next;
} else {
p = p.next;
}
}
return head;
}
}