AcWing 29. 删除链表中重复的节点-JAVA
原题链接
中等
作者:
joyonline
,
2019-12-12 21:42:08
,
所有人可见
,
阅读 812
java 代码
class Solution {
public ListNode deleteDuplication(ListNode head) {
if (head == null) {
return null;
}
ListNode list = head;
int tmp = -1;
ListNode pre = null;
while(head.next!=null){
ListNode frist = head;
ListNode second = head.next;
if(frist.val == second.val){//删除节点
tmp = head.val;
frist.val = second.val;
frist.next = second.next;
}else if(frist.val==tmp){
frist.val = second.val;
frist.next = second.next;
}else{
pre = head;//前一个节点
head = head.next;//当前节点
}
if(head.next==null && head.val == tmp){//删除尾节点方法,是将倒数第二个节点的next设置为null
if(pre!=null){
pre.next = null;
}else{
return null;
}
}
}
return list;
}
}