面试题 02.01. 移除重复节点

面试题 02.01. 移除重复节点
hash(O(n))

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeDuplicateNodes(ListNode head) {
        int[] hash = new int[20005];
        ListNode prev = head, curr = head;
        while (curr != null) {
            if (hash[curr.val] == 0) {
                hash[curr.val] ++;
                prev = curr;
            } else {
                prev.next = curr.next;
            }
            curr = curr.next;
        }
        return head;
    }
}
上一篇:【代码笔记】测量模型推理时间


下一篇:队列和符号表要怎么实现?