Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
遍历一遍即可,重复则删除
public ListNode deleteDuplicates(ListNode head)
{
if(head==null)
return head;
int mark = head.val;
ListNode pre = head;
ListNode point = head.next;
while(point!=null)
{
if(point.val==mark)
{
//remove the node
pre.next = point.next;
point = point.next;
}else
{
mark = point.val;
pre = point;
point = point.next;
}
}
return head;
}
本文介绍了一种简单的方法来删除已排序链表中的重复元素,确保每个元素只出现一次。通过遍历链表并比较相邻节点值的方式实现,当发现重复元素时立即删除。

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



