3217力扣 在链表中移除在数组中存在的节点

/**
 * 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) {
        set<int> st(nums.begin(),nums.end());
        ListNode* h=new ListNode();
        ListNode* p1=h;
        while(head!=nullptr){
            ListNode* nx=head->next;
            if(!st.count(head->val)){
                p1->next=head;
                p1=p1->next;
                p1->next=nullptr;
            }
            head=nx;
        }
        return h->next;
    }    
};

利用集合查找元素、提早设置一个next节点用于保存下一个查找节点、要设置尾部节点的next为nullptr

相关推荐

  1. LeetCode——2487. 从节点

    2024-07-16 17:50:02       45 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-16 17:50:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-16 17:50:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-16 17:50:02       57 阅读
  4. Python语言-面向对象

    2024-07-16 17:50:02       68 阅读

热门阅读

  1. Qt 实战(7)元对象系统 | 7.3、QMetaObject

    2024-07-16 17:50:02       20 阅读
  2. 交易积累——VR

    2024-07-16 17:50:02       22 阅读
  3. 艺术类硕士在核心期刊上发表论文真的很难么?

    2024-07-16 17:50:02       23 阅读
  4. 靖江美食元宇宙

    2024-07-16 17:50:02       18 阅读
  5. 1213:八皇后问题

    2024-07-16 17:50:02       19 阅读
  6. py每日spider案例之影视搜索篇

    2024-07-16 17:50:02       19 阅读
  7. Linux内核 -- 用户态coredump处理之do_coredump函数

    2024-07-16 17:50:02       24 阅读
  8. 什么是MATLAB许可证协议书

    2024-07-16 17:50:02       22 阅读