leetcode 203 移除链表元素

简介: leetcode 203 移除链表元素

ba3ad9e723034f14b80bb65216b22001.png

第一版

容易出现操作空指针,要为对应的特殊情况做处理

直接在原本的链表上做处理。

分为两种情况

  • 删除链表头
  • 删除非表头

核心代码

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL)return NULL;
        ListNode* temp, * tempnext, * Head = head, * dele_val;
        while (Head->val == val)
        {
            dele_val = Head;
            if (Head->next != nullptr)Head = Head->next;
            else return NULL;
            delete dele_val;
        }
        temp = Head;
        tempnext = temp->next;
        if (tempnext == nullptr)return Head;
        while (tempnext->next != nullptr)
        {
            if (tempnext->val == val)
            {
                temp->next = tempnext->next;
                dele_val = tempnext;
                delete dele_val;
                tempnext = temp->next;
            }
            else
            {
                temp = temp->next;
                tempnext = temp->next;
            }
        }
        if (tempnext->val == val)
        {
            temp->next = nullptr;
            delete tempnext;
        }
        return Head;
    }
};

全部代码

#include <iostream>
#include <vector>
#include<algorithm> 
using namespace std;
  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 LinkList
  {
  public:
      ListNode* head;
      LinkList();         //构造
      ~LinkList();        //析构
      void print();       //打印链表元素
      inline int getLength()
      {
          return length;
      };
      void pushBack(int data);    //尾插
  private:
      int length;
  };
  LinkList::LinkList()
  {
      head = new ListNode();
      head->next = nullptr;
      head->val = NULL;
      length = 0;
  }
  LinkList::~LinkList()
  {
      if (length == 0)
      {
          delete head;    //删除头节点空间
          head = nullptr;
          return;
      }
      ListNode* p = head->next;
      delete head;
      while (p != nullptr)
      {
          ListNode* tmpptr = p->next;
          delete p;
          p = tmpptr;
      }
      length = 0;
  }
  void LinkList::pushBack(int data)    //尾插
  {
      ListNode* temp = head;
      ListNode* cur = new ListNode();
      cur->val = data;
      cur->next = nullptr;
          while (temp->next != nullptr)
          {
              temp = temp->next;
          }
          temp->next = cur;
      length++;
      return;
  }
  void LinkList::print()
  {
      if (length == 0) return;
      ListNode* temp = head->next;
      cout << "current list:" << endl;
      int i;
      while (temp->next!= nullptr)
      {
          cout << temp->val << ' ';
          temp = temp->next;
      }
      cout << temp->val << ' ';
      cout << endl;
      temp = nullptr;
  }
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if (head == NULL)return NULL;
        ListNode* temp, * tempnext, * Head = head, * dele_val;
        while (Head->val == val)
        {
            dele_val = Head;
            if (Head->next != nullptr)Head = Head->next;
            else return NULL;
            delete dele_val;
        }
        temp = Head;
        tempnext = temp->next;
        if (tempnext == nullptr)return Head;
        while (tempnext->next != nullptr)
        {
            if (tempnext->val == val)
            {
                temp->next = tempnext->next;
                dele_val = tempnext;
                delete dele_val;
                tempnext = temp->next;
            }
            else
            {
                temp = temp->next;
                tempnext = temp->next;
            }
        }
        if (tempnext->val == val)
        {
            temp->next = nullptr;
            delete tempnext;
        }
        return Head;
    }
};
int main()
{
    vector<int> head = { 1 };
    int val = 2;
    LinkList TestList , TestList2;
    Solution  a;
    for (int i = 0; i < head.size(); i++)
    {
        TestList.pushBack(head[i]);
    }
    TestList.print();
    ListNode* temp = a.removeElements(TestList.head->next, val);
    TestList.head->next = temp;
    cout << "last list:" << endl;
    while (temp->next != nullptr)
    {
        cout  << temp->val << ' ';
        temp = temp->next;
    }
    cout << temp->val << ' ';
    cout << endl;
  return 0;
}


虚拟头节点法

主动在链表之前加一个虚拟头,这样将删除头节点和删除其他节点合并成一种类型

核心代码

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* DummyHead = new ListNode(0 , head);
        ListNode* cur = DummyHead;
        while (cur->next != nullptr)
        {
            if (cur->next->val == val) 
            {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }
            else
            {
                cur = cur->next;
            }
        }
        head = DummyHead->next;
        delete DummyHead;
        return head;
    }
};


全部代码

#include <iostream>
#include <vector>
#include<algorithm> 
using namespace std;
  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 LinkList
  {
  public:
      ListNode* head;
      LinkList();         //构造
      ~LinkList();        //析构
      void print();       //打印链表元素
      inline int getLength()
      {
          return length;
      };
      void pushBack(int data);    //尾插
  private:
      int length;
  };
  LinkList::LinkList()
  {
      head = new ListNode();
      head->next = nullptr;
      head->val = NULL;
      length = 0;
  }
  LinkList::~LinkList()
  {
      if (length == 0)
      {
          delete head;    //删除头节点空间
          head = nullptr;
          return;
      }
      ListNode* p = head->next;
      delete head;
      while (p != nullptr)
      {
          ListNode* tmpptr = p->next;
          delete p;
          p = tmpptr;
      }
      length = 0;
  }
  void LinkList::pushBack(int data)    //尾插
  {
      ListNode* temp = head;
      ListNode* cur = new ListNode();
      cur->val = data;
      cur->next = nullptr;
          while (temp->next != nullptr)
          {
              temp = temp->next;
          }
          temp->next = cur;
      length++;
      return;
  }
  void LinkList::print()
  {
      if (length == 0) return;
      ListNode* temp = head->next;
      cout << "current list:" << endl;
      int i;
      while (temp->next!= nullptr)
      {
          cout << temp->val << ' ';
          temp = temp->next;
      }
      cout << temp->val << ' ';
      cout << endl;
      temp = nullptr;
  }
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* DummyHead = new ListNode(0 , head);
        ListNode* cur = DummyHead;
        while (cur->next != nullptr)
        {
            if (cur->next->val == val) 
            {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                delete temp;
            }
            else
            {
                cur = cur->next;
            }
        }
        head = DummyHead->next;
        delete DummyHead;
        return head;
    }
};
int main()
{
    vector<int> head = { 1 };
    int val = 2;
    LinkList TestList , TestList2;
    Solution  a;
    for (int i = 0; i < head.size(); i++)
    {
        TestList.pushBack(head[i]);
    }
    TestList.print();
    ListNode* temp = a.removeElements(TestList.head->next, val);
    TestList.head->next = temp;
    cout << "last list:" << endl;
    while (temp->next != nullptr)
    {
        cout  << temp->val << ' ';
        temp = temp->next;
    }
    cout << temp->val << ' ';
    cout << endl;
  return 0;
}


二刷

虚拟头节点

/**
 * 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* removeElements(ListNode* head, int val) {
        ListNode *duny_head = new ListNode(0,head);
        ListNode *cur = duny_head;
        while(cur->next != nullptr)
        {
            if(cur->next->val == val) 
            {
                ListNode *tmp = cur->next;
                cur->next = cur->next->next;
                delete tmp;
            }else
                cur = cur->next;
        }
        ListNode *result = duny_head->next;
        delete duny_head;
        return result;
    }
};


相关文章
|
2月前
【力扣】-- 移除链表元素
【力扣】-- 移除链表元素
37 1
|
2月前
Leetcode第21题(合并两个有序链表)
这篇文章介绍了如何使用非递归和递归方法解决LeetCode第21题,即合并两个有序链表的问题。
52 0
Leetcode第21题(合并两个有序链表)
|
2月前
【LeetCode 27】347.前k个高频元素
【LeetCode 27】347.前k个高频元素
38 0
|
2月前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
25 0
LeetCode第二十四题(两两交换链表中的节点)
|
2月前
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第19题要求删除链表的倒数第N个节点,可以通过快慢指针法在一次遍历中实现。
44 0
Leetcode第十九题(删除链表的倒数第N个节点)
|
2月前
|
索引
力扣(LeetCode)数据结构练习题(3)------链表
力扣(LeetCode)数据结构练习题(3)------链表
96 0
|
2月前
【LeetCode 10】142. 环形链表 II
【LeetCode 10】142. 环形链表 II
23 0
|
2月前
【LeetCode 09】19 删除链表的倒数第 N 个结点
【LeetCode 09】19 删除链表的倒数第 N 个结点
18 0
|
2月前
【LeetCode 08】206 反转链表
【LeetCode 08】206 反转链表
13 0
|
3月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行