感觉c++写数据结构好tm蠢,我要用Java了草
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeDuplicateNodes(ListNode head) {
if(head==null)return head;
ListNode res=head;
HashSet<Integer>set = new HashSet<>();
//我们对给定的链表进行一次遍历,并用一个哈希集合(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;
}
}
return res;
}
};