comments | difficulty | edit_url |
---|---|---|
true |
简单 |
编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
示例1:
输入:[1, 2, 3, 3, 2, 1] 输出:[1, 2, 3]
示例2:
输入:[1, 1, 1, 1, 2] 输出:[1, 2]
提示:
- 链表长度在[0, 20000]范围内。
- 链表元素在[0, 20000]范围内。
进阶:
如果不得使用临时缓冲区,该怎么解决?
我们创建一个哈希表
然后我们创建一个虚拟节点
接下来我们遍历链表,如果当前节点的值已经在哈希表中,我们就将当前节点删除,即
遍历结束后,我们返回链表的头节点。
时间复杂度
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeDuplicateNodes(self, head: ListNode) -> ListNode:
vis = set()
pre = ListNode(0, head)
while pre.next:
if pre.next.val in vis:
pre.next = pre.next.next
else:
vis.add(pre.next.val)
pre = pre.next
return head
/**
* 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;
}
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeDuplicateNodes(ListNode* head) {
unordered_set<int> vis;
ListNode* pre = new ListNode(0, head);
while (pre->next) {
if (vis.count(pre->next->val)) {
pre->next = pre->next->next;
} else {
vis.insert(pre->next->val);
pre = pre->next;
}
}
return head;
}
};
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeDuplicateNodes(head *ListNode) *ListNode {
vis := map[int]bool{}
pre := &ListNode{0, head}
for pre.Next != nil {
if vis[pre.Next.Val] {
pre.Next = pre.Next.Next
} else {
vis[pre.Next.Val] = true
pre = pre.Next
}
}
return head
}
/**
* 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 removeDuplicateNodes(head: ListNode | null): ListNode | null {
const vis: Set<number> = new Set();
let pre: ListNode = new ListNode(0, head);
while (pre.next) {
if (vis.has(pre.next.val)) {
pre.next = pre.next.next;
} else {
vis.add(pre.next.val);
pre = pre.next;
}
}
return head;
}
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
use std::collections::HashSet;
impl Solution {
pub fn remove_duplicate_nodes(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut vis = HashSet::new();
let mut pre = ListNode::new(0);
pre.next = head;
let mut cur = &mut pre;
while let Some(node) = cur.next.take() {
if vis.contains(&node.val) {
cur.next = node.next;
} else {
vis.insert(node.val);
cur.next = Some(node);
cur = cur.next.as_mut().unwrap();
}
}
pre.next
}
}
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var removeDuplicateNodes = function (head) {
const vis = new Set();
let pre = new ListNode(0, head);
while (pre.next) {
if (vis.has(pre.next.val)) {
pre.next = pre.next.next;
} else {
vis.add(pre.next.val);
pre = pre.next;
}
}
return head;
};
/**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ x: Int, _ next: ListNode? = nil) {
* self.val = x
* self.next = next
* }
* }
*/
class Solution {
func removeDuplicateNodes(_ head: ListNode?) -> ListNode? {
var vis = Set<Int>()
let pre = ListNode(0, head)
var current: ListNode? = pre
while current?.next != nil {
if vis.insert(current!.next!.val).inserted {
current = current?.next
} else {
current?.next = current?.next?.next
}
}
return head
}
}