comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
给定一个链表的第一个节点 head
,找到链表中所有出现多于一次的元素,并删除这些元素所在的节点。
返回删除后的链表。
示例 1:
输入: head = [1,2,3,2] 输出: [1,3] 解释: 2 在链表中出现了两次,所以所有的 2 都需要被删除。删除了所有的 2 之后,我们还剩下 [1,3] 。
示例 2:
输入: head = [2,1,1,2] 输出: [] 解释: 2 和 1 都出现了两次。所有元素都需要被删除。
示例 3:
输入: head = [3,2,2,1,3,2,4] 输出: [1,4] 解释: 3 出现了两次,且 2 出现了三次。移除了所有的 3 和 2 后,我们还剩下 [1,4] 。
提示:
- 链表中节点个数的范围是
[1, 105]
1 <= Node.val <= 105
我们可以用哈希表
时间复杂度
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def deleteDuplicatesUnsorted(self, head: ListNode) -> ListNode:
cnt = Counter()
cur = head
while cur:
cnt[cur.val] += 1
cur = cur.next
dummy = ListNode(0, head)
pre, cur = dummy, head
while cur:
if cnt[cur.val] > 1:
pre.next = cur.next
else:
pre = cur
cur = cur.next
return dummy.next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicatesUnsorted(ListNode head) {
Map<Integer, Integer> cnt = new HashMap<>();
for (ListNode cur = head; cur != null; cur = cur.next) {
cnt.put(cur.val, cnt.getOrDefault(cur.val, 0) + 1);
}
ListNode dummy = new ListNode(0, head);
for (ListNode pre = dummy, cur = head; cur != null; cur = cur.next) {
if (cnt.get(cur.val) > 1) {
pre.next = cur.next;
} else {
pre = cur;
}
}
return dummy.next;
}
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicatesUnsorted(ListNode* head) {
unordered_map<int, int> cnt;
for (ListNode* cur = head; cur; cur = cur->next) {
cnt[cur->val]++;
}
ListNode* dummy = new ListNode(0, head);
for (ListNode *pre = dummy, *cur = head; cur; cur = cur->next) {
if (cnt[cur->val] > 1) {
pre->next = cur->next;
} else {
pre = cur;
}
}
return dummy->next;
}
};
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicatesUnsorted(head *ListNode) *ListNode {
cnt := map[int]int{}
for cur := head; cur != nil; cur = cur.Next {
cnt[cur.Val]++
}
dummy := &ListNode{0, head}
for pre, cur := dummy, head; cur != nil; cur = cur.Next {
if cnt[cur.Val] > 1 {
pre.Next = cur.Next
} else {
pre = cur
}
}
return dummy.Next
}
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function deleteDuplicatesUnsorted(head: ListNode | null): ListNode | null {
const cnt: Map<number, number> = new Map();
for (let cur = head; cur; cur = cur.next) {
const x = cur.val;
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}
const dummy = new ListNode(0, head);
for (let pre = dummy, cur = head; cur; cur = cur.next) {
if (cnt.get(cur.val)! > 1) {
pre.next = cur.next;
} else {
pre = cur;
}
}
return dummy.next;
}