100368. 从链表中移除在数组中存在的节点
给你一个整数数组 nums
和一个链表的头节点 head
。从链表中移除所有存在于 nums
中的节点后,返回修改后的链表的头节点。
示例 1:
输入: nums = [1,2,3], head = [1,2,3,4,5]
输出: [4,5]
解释:
移除数值为 1, 2 和 3 的节点。
示例 2:
输入: nums = [1], head = [1,2,1,2,1,2]
输出: [2,2,2]
解释:
移除数值为 1 的节点。
示例 3:
输入: nums = [5], head = [1,2,3,4]
输出: [1,2,3,4]
解释:
链表中不存在值为 5 的节点。
提示:
1 <= nums.length <= 105
1 <= nums[i] <= 105
nums
中的所有元素都是唯一的。[1, 105]
的范围内。1 <= Node.val <= 105
nums
中出现过。原站题解
php 解法, 执行用时: 322 ms, 内存消耗: 57.8 MB, 提交时间: 2024-07-15 09:53:45
/** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param Integer[] $nums * @param ListNode $head * @return ListNode */ function modifiedList($nums, $head) { $set = array_flip($nums); $dummy = new ListNode(0, $head); $cur = $dummy; while ( $cur->next !== null ) { if ( isset($set[$cur->next->val]) ) { $cur->next = $cur->next->next; } else { $cur = $cur->next; } } return $dummy->next; } }
javascript 解法, 执行用时: 422 ms, 内存消耗: 88.6 MB, 提交时间: 2024-07-15 09:45:04
/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {number[]} nums * @param {ListNode} head * @return {ListNode} */ var modifiedList = function(nums, head) { // 创建一个哈希集合来存储nums中的元素 const has = new Set(nums); // 创建一个哑节点(dummy node)来简化头节点的处理 const dummy = new ListNode(0, head); let cur = dummy; // 遍历链表 while (cur.next !== null) { // 如果当前节点的值在nums数组中,则删除该节点 if (has.has(cur.next.val)) { cur.next = cur.next.next; } else { // 否则,移动到下一个节点 cur = cur.next; } } // 返回修改后的链表的头节点(跳过哑节点) return dummy.next; };
golang 解法, 执行用时: 252 ms, 内存消耗: 11.3 MB, 提交时间: 2024-07-15 09:42:26
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func modifiedList(nums []int, head *ListNode) *ListNode { has := make(map[int]bool, len(nums)) // 预分配空间 for _, x := range nums { has[x] = true } dummy := &ListNode{Next: head} cur := dummy for cur.Next != nil { if has[cur.Next.Val] { cur.Next = cur.Next.Next // 删除 } else { cur = cur.Next // 向后移动 } } return dummy.Next }
java 解法, 执行用时: 19 ms, 内存消耗: 65.1 MB, 提交时间: 2024-07-15 09:42:06
/** * 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 modifiedList(int[] nums, ListNode head) { Set<Integer> set = new HashSet<>(nums.length); // 预分配空间 for (int x : nums) { set.add(x); } ListNode dummy = new ListNode(0, head); ListNode cur = dummy; while (cur.next != null) { if (set.contains(cur.next.val)) { cur.next = cur.next.next; // 删除 } else { cur = cur.next; // 向后移动 } } return dummy.next; } }
cpp 解法, 执行用时: 474 ms, 内存消耗: 256.7 MB, 提交时间: 2024-07-15 09:41:54
/** * 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* modifiedList(vector<int>& nums, ListNode* head) { unordered_set<int> st(nums.begin(), nums.end()); ListNode dummy(0, head); ListNode* cur = &dummy; while (cur->next) { auto nxt = cur->next; if (st.contains(nxt->val)) { cur->next = nxt->next; // 删除 delete nxt; // 释放内存 } else { cur = nxt; // 向后移动 } } return dummy.next; } };
cpp 解法, 执行用时: 445 ms, 内存消耗: 252.5 MB, 提交时间: 2024-07-15 09:41:45
/** * 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* modifiedList(vector<int>& nums, ListNode* head) { unordered_set<int> st(nums.begin(), nums.end()); ListNode dummy(0, head); ListNode* cur = &dummy; while (cur->next) { if (st.contains(cur->next->val)) { cur->next = cur->next->next; // 删除 } else { cur = cur->next; // 向后移动 } } return dummy.next; } };
python3 解法, 执行用时: 364 ms, 内存消耗: 56.4 MB, 提交时间: 2024-07-15 09:41:28
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]: st = set(nums) cur = dummy = ListNode(next=head) while cur.next: if cur.next.val in st: cur.next = cur.next.next # 删除 else: cur = cur.next # 向后移动 return dummy.next