For example,
Given
Given
1->1->2,
return 1->2.Given 1->1->2->3->3,
return 1->2->3.
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null) return null;
ListNode a = head;
while(a.next!=null){
if(a.val==a.next.val){
a.next = a.next.next;
}else{
a = a.next;
}
}
return head;
}
}

本文介绍了一种算法,用于删除链表中的重复元素。通过遍历链表并比较节点值,可以有效地去除重复项,确保每个节点只出现一次。
1138

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



