在刷删除链表重复元素一题时,碰到了问题,求大神帮助呀
leetcode吧
全部回复
仅看楼主
level 1
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null)
throw new IllegalArgumentException("the number of nodes must be more than one.");
ListNode head0=head;
ListNode cur=head0.next ;
if(head0==cur){
head0=cur;
}
while(cur.next!=null){
System.out.println(cur);
if(cur==cur.next){
ListNode node=cur.next;
cur.next=node.next;
node.next=null;
}else
cur=cur.next;
}
return head0;
}
方法运行链表不变化 啊
2019年10月22日 08点10分 1
level 1
2019年10月22日 08点10分 2
level 1
给你个提示,90%的链表题,加一个虚头节点会好做很多:
ListNode vhead = new ListNode(0);
vhead.next=head;
var prev=vhead;
var p=head;
2020年02月17日 12点02分 5
1