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;
    }
};


相关文章
|
6天前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
1天前
|
Java Python
二刷力扣--链表
二刷力扣--链表
|
2天前
|
算法
【经典LeetCode算法题目专栏分类】【第10期】排序问题、股票问题与TOP K问题:翻转对、买卖股票最佳时机、数组中第K个最大/最小元素
【经典LeetCode算法题目专栏分类】【第10期】排序问题、股票问题与TOP K问题:翻转对、买卖股票最佳时机、数组中第K个最大/最小元素
|
2天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
|
6天前
|
存储 算法 数据可视化
深入解析力扣160题:相交链表的解决方法(哈希表法与双指针法详细图解)
深入解析力扣160题:相交链表的解决方法(哈希表法与双指针法详细图解)
|
6天前
|
SQL 算法 数据可视化
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
|
6天前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
6天前
|
存储 SQL 算法
LeetCode 83题:删除排序链表中的重复元素【面试】
LeetCode 83题:删除排序链表中的重复元素【面试】
|
1天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
1天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题