public class ListNode
{
int val;
ListNode next;
ListNode(int val)
{
this.val = val;
}
}
public class ReDupSortList
{
ReDupSortList(){}
public ListNode deleteDuplicates(ListNode head)
{
if(head == null)
{return head;}
ListNode result= head;
ListNode pre= head;
head=head.next;
while(head != null)
{
if(head.val == pre.val)
{
pre.next= head.next;
head= pre.next;
}
else
{
pre= pre.next;
head= head.next;
}
}
return result;
}
}
本文介绍了一种算法,用于删除已排序链表中的所有重复元素,仅保留每个节点值的第一个实例。通过遍历链表并比较相邻节点的值来实现这一目标,如果发现重复,则跳过重复节点。

1608

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



