/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param ListNode head is the head of the linked list
* @return: ListNode head of linked list
*/
public static ListNode deleteDuplicates(ListNode head) {
// write your code here
if(head==null){
return null;
}
ListNode list=head;
while(list.next!=null){
if(list.val==list.next.val){
if(list.next.next==null){
list.next=null;
}else{
ListNode node=list.next;
list.next=node.next;
}
}else{
list=list.next;
}
}
return head;
}
}移除有序链表中的重复元素,保留一个重复值
最新推荐文章于 2025-10-06 22:46:42 发布
本文介绍了一种算法,用于删除带有重复值的链表中的重复节点。通过遍历链表并比较相邻节点的值来实现,若发现重复则跳过重复节点。此方法能有效清理链表中的重复数据。
1083

被折叠的 条评论
为什么被折叠?



