leetcode第83题目
输入的链表是1,1,2
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head==null) return head;
ListNode a=head;
while (a.next!=null){
if (a.val==a.next.val){
a.next=a.next.next;
}
}
return head;
}
}
这样写没有问题
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head==null) return head;
while (head.next!=null){
if (head.val==head.next.val){
head.next=head.next.next;
}
}
return head;
}
}
这样写输出的是一个1了结果不对
你不用另一个指向head的话,你遍历每次要head=head.next,最终你返回是head都不是指向最初的节点了
明白了,因为在最后返回了head时返回的是在2的那个点
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!