面试题 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;
}
}
标签:面试题,ListNode,val,int,02.01,head,next,移除,curr 来源: https://blog.csdn.net/e935052319/article/details/117201816