• leetcode 203


    struct ListNode* removeElements(struct ListNode* head, int val) 
    {
        struct ListNode * tmp = (struct  ListNode *)malloc(sizeof(struct ListNode));
        struct ListNode * pre = tmp;
        pre->next = head;
        while(head != NULL)
        { 
            if(head->val == val) { 
                head= head->next;
                pre->next = head;
            }else{
                head = head->next;
                pre = pre->next;
            } 
        } 
        return tmp->next;
    }

    基本思路:

    在删某个节点的时候需要修改该节点前面节点的next指针,所以需要有一个pre指针

    另外需要有个节点来保存节点最初的位置 。

    struct ListNode* removeElements(struct ListNode* head, int val)
    {
        struct ListNode * tmp = (struct  ListNode *)malloc(sizeof(struct ListNode));
        struct ListNode * pre = tmp;
        pre->next = head;
        while(head != NULL)
        {
            if(head->val == val) {
                head= head->next;
                pre->next = head;
            }else{
                head = head->next;
                pre = pre->next;
            }
        }
        return tmp->next;
    }

  • 相关阅读:
    PostgreSQL数据损坏与checksum
    go get命令无响应解决方法
    vscode离线安装插件
    windows搭建Go语言环境
    centos7安装zabbix 5.0
    搭建人生重开模拟器
    博客园增加打赏
    记一次halo报错
    VM operation inconsistent with current state
    nextcloud安装onlyoffice
  • 原文地址:https://www.cnblogs.com/cdwodm/p/5164700.html
Copyright © 2020-2023  润新知