作者:捕风的默小墨 | 来源:互联网 | 2023-02-06 06:54
1> v78..:
您正在删除重复的节点,而不是您发布的代码中的重复节点.
/*
DEFINITIONS
public class ListNode {
int val;
ListNode next;
public ListNode(int x){
val = x;
next = null;
}
}
public class LinkedList {
ListNode head;
}
*/
public void removeDups(){
ListNode head = this.head;
if(head == null) return;
HashSet set = new HashSet();
set.add(head.val);
while(head.next != null){
if(set.contains(head.next.val)){
head.next = head.next.next;
} else{
set.add(head.next.val);
head = head.next;
}
}
}