代码随想录算法训练营第三天| 203.移除链表元素 、 707.设计链表 、206.反转链表

链表的构造:

link.h:

 1 #ifndef LINK_H
 2 #define LINK_H
 3 #include<vector>
 4 
 5 struct ListNode {
 6     int val;
 7     ListNode* next;
 8     ListNode() :val(0), next(nullptr) {}
 9     ListNode(int x) : val(x), next(nullptr) {}
10     ListNode(int x, ListNode* next) : val(x), next(next) {}
11 };
12 
13 class Solution {
14 public:
15     ListNode* removeElements(ListNode* head, int val);
16 
17 };
18 #endif
Link.h

实现链表:

1 #include<iostream>
2 #include"Link.h"
3 using namespace std;
4 
5 ListNode* Solution::removeElements(ListNode* head, int val)
6 {}
Complement

实现移除元素:

难点:

  1,因为给的head是不是空节点的head,一定要注意这一点

  2,需要自己定义一个空的头节点

  3,需要设置一个游标

  4,返回的时候,返回空节点的时候->用num

 1 ListNode* Solution::removeElements(ListNode* head, int val)
 2 {
 3     ListNode* DyHead = new ListNode();
 4     //注意 cur_ == head 
 5     DyHead->next = head;
 6     auto cur_ = DyHead;
 7 
 8     while (cur_->next != nullptr)
 9     {
10         if (cur_->next->val == val)
11         {
12             ListNode* mid = cur_->next;
13             cur_->next = mid->next;
14         }
15         // 不是的话,就下一个
16         else 
17         {
18             cur_ = cur_->next;
19         }
20     
21     }
22 
23     //不是原数组的head 而是 dyhead->next
24     return DyHead->next;
25 }

 

设计列表 ,仍然出现访问空指针的问题,需要查看

class MyLinkedList {
public:
struct LinkNode
    {
        int val;
        LinkNode* next;
        LinkNode(int v) : val(v), next(nullptr) {};
    };
    MyLinkedList() {
    head = new LinkNode(0);
    head->next = nullptr;

    }
    
    int get(int index) {
auto cur_ = head;

    int idx = 0;
    while (cur_->next != nullptr)
    {
        if (idx == index)
        {
            return cur_->next->val;
        }
        else
        {
            cur_ = cur_->next;
        }

        idx++;
    }

    return -1;
    }
    
    void addAtHead(int val) {
    auto* cur = head->next;
    LinkNode* mid = new LinkNode(val);
    head->next = mid;
    mid->next = cur;
    }
    
    void addAtTail(int val) {
    LinkNode* mid = new LinkNode(val);
    auto* cur = head;
    while (cur->next != nullptr)
    {
        cur = cur->next;
    }

    cur->next = mid;
    }
    
    void addAtIndex(int index, int val) {
int length = 0, idx = 0;
    LinkNode* cur = head;
    LinkNode* newNode = new LinkNode(val);

    // 一定要把循环写进去,因为while(index--),最后一层,会把index -- 多一步
    while (index > 0)
    {
        if (cur->next != nullptr)
        {
            cur = cur->next;
        }
        else
        {
            break;
        }
        index = index - 1;
    }

    if (index == 0 && cur->next != nullptr)
    {
        LinkNode* mid = cur->next;
        cur->next = newNode;
        newNode->next = mid;
    }
    else if (index == 0 && cur->next == nullptr)
    {
        cur->next = newNode;
    }
    }
    
    void deleteAtIndex(int index) {
LinkNode* cur = head;
    while (index >0)
    {
        if (cur->next != nullptr)
        {
            cur = cur->next;
        }
        else
        {
            break;
        }
        index = index - 1;
    }

    if (index == 0)
    {
        cur->next = cur->next->next;
    }
    }
    private:
    LinkNode* head;
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList* obj = new MyLinkedList();
 * int param_1 = obj->get(index);
 * obj->addAtHead(val);
 * obj->addAtTail(val);
 * obj->addAtIndex(index,val);
 * obj->deleteAtIndex(index);
 */

 

反转列表:

 1 ListNode* MyLinkedList::reverseList(ListNode* head) {
 2     ListNode* dyHead = new ListNode();
 3     dyHead->next = head;
 4     auto* cur = dyHead;
 5     
 6     ListNode* result = new ListNode();
 7     while (cur->next !=nullptr)
 8     {
 9         ListNode* currentNode = new ListNode(cur->next->val);
10         auto mid = result->next;
11         result->next = currentNode;
12         currentNode->next = mid;
13 
14         cur = cur->next;
15     }
16 
17     return result->next;
18 }

 

posted @ 2023-06-09 23:13  博二爷  阅读(958)  评论(0编辑  收藏  举报