AcWing 29. 删除链表中重复的节点
原题链接
简单
作者:
小轩喵灬
,
2025-01-11 15:41:06
,
所有人可见
,
阅读 1
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplication(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
if (head.val == next.val) {
while(next != null && head.val ==next.val) {
next = next.next;
}
return deleteDuplication(next);
} else {
head.next = deleteDuplication(head.next);
return head;
}
}
}