【c++刷题笔记-链表】day03: leetcode203.移除链表元素 , leetcode707.设计链表 , leetcode206.反转链表

leetcode203

思路:创建新的节点做哨兵节点,让哨兵节点的next指向传来的头节点,这样删除第一个节点就和其他节点一样的操作了

重点:需要一个新的节点接受哨兵节点,再进行遍历

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

leetcode707

思路:需要自己创建一个单链表,按照每个方法的要求完成对应的功能

重点:链表遍历的时候需要一个临时节点,不要在原始节点上操作,链表删除注意野指针

class MyLinkedList {
public:
    struct LinkedNode{//创建单链表
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val),next(nullptr){}
    };
    MyLinkedList() {
        size=0;
        node=new LinkedNode(0);
    }
    
    int get(int index) {
        if(index<0||index>size-1){
            return -1;
        }
        LinkedNode* cur=node->next;
        while(index--){
            cur=cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedNode* newnode=new LinkedNode(val);
        newnode->next=node->next;
        node->next=newnode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* newnode=new LinkedNode(val);
        LinkedNode* cur=node;
        while(cur->next!=nullptr){
            cur=cur->next;
        }
        cur->next=newnode;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > size) {
            return;
        }
        if(index < 0){
            index=0;
        }     
        LinkedNode* newNode = new LinkedNode(val);
        LinkedNode* cur = node;
        while(index--) {
            cur = cur->next;
        }
        newNode->next = cur->next;
        cur->next = newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
         if (index >= size || index < 0) {
            return;
        }
        LinkedNode* cur = node;
        while(index--) {
            cur = cur ->next;
        }
        LinkedNode* tmp = cur->next;
        cur->next = cur->next->next;
        delete tmp;//野指针
        tmp=nullptr;
        size--;
    }
private:
    int size;
    LinkedNode* node;    
};

leetcode206

思路:定义头节点,头节点记录前一个节点的值,当前节点向后遍历,每遍历一次就修改一次当前指针指向

重点:指针指向弄清楚,cur->next=pre;   pre=cur;指针每次都到当前节点的前一个节点

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre=nullptr;
        ListNode* cur=head;
        while(cur!=nullptr){
            ListNode* tmp=cur->next;//记录要操作的节点的下一个节点
            cur->next=pre;//把当前节点的next指向前一个节点
            pre=cur;//下一个要操作节点变为当前节点
            cur=tmp;//当前节点继续按照原来的链表进行遍历
        }
        return pre;
    }
};

总结

链表基本都可以用虚拟头节点,所以要善用虚拟头结点。链表问题需要定义一个临时节点,尽量不要在原始节点进行操作。需要梳理好指针方向,保存下一个待操作的节点,可以把这个节点当成一个新的链表的头结点使用。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值