返回介绍

solution / 1800-1899 / 1836.Remove Duplicates From an Unsorted Linked List / README_EN

发布于 2024-06-17 01:03:14 字数 6080 浏览 0 评论 0 收藏 0

1836. Remove Duplicates From an Unsorted Linked List

中文文档

Description

Given the head of a linked list, find all the values that appear more than once in the list and delete the nodes that have any of those values.

Return _the linked list after the deletions._

 

Example 1:


Input: head = [1,2,3,2]

Output: [1,3]

Explanation: 2 appears twice in the linked list, so all 2's should be deleted. After deleting all 2's, we are left with [1,3].

Example 2:


Input: head = [2,1,1,2]

Output: []

Explanation: 2 and 1 both appear twice. All the elements should be deleted.

Example 3:


Input: head = [3,2,2,1,3,2,4]

Output: [1,4]

Explanation: 3 appears twice and 2 appears three times. After deleting all 3's and 2's, we are left with [1,4].

 

Constraints:

  • The number of nodes in the list is in the range [1, 105]
  • 1 <= Node.val <= 105

Solutions

Solution 1: Hash Table

We can use a hash table $cnt$ to count the number of occurrences of each element in the linked list, and then traverse the linked list to delete elements that appear more than once.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the linked list.

# 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文