leetcode/lcci/02.01.Remove Duplicate Node/Solution.java

22 lines
540 B
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) {
Set<Integer> vis = new HashSet<>();
ListNode pre = new ListNode(0, head);
while (pre.next != null) {
if (vis.add(pre.next.val)) {
pre = pre.next;
} else {
pre.next = pre.next.next;
}
}
return head;
}
}