代码随想录算法训练营第三天|LeetCode203. 移除链表元素,LeetCode707. 设计链表,LeetCode206. 反转链表

LeetCode203. 移除链表元素

题目链接:203. 移除链表元素 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:手把手带你学会操作链表 | LeetCode:203.移除链表元素_哔哩哔哩_bilibili

思路1:将删除头结点的情况和删除后续节点的情况分开讨论,如果是头结点的值恰好等于val,那么需要将头结点后移,并把原先的头结点删除。在上述处理完成后,再讨论之后的节点。

代码:

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while(head!=NULL&&head->val==val){
            ListNode *tmp=head;
            head=head->next;
            delete tmp;
        }
        ListNode *cur=head;
        while(cur!=NULL&&cur->next!=NULL){
            if(cur->next->val==val){
                ListNode *tmp2=cur->next;
                cur->next=cur->next->next;
                delete tmp2;
            }
            else{
                cur=cur->next;
            }
        }
        return head;
    }
};

思路2:创建一个虚拟头结点,他的next指向当前的头结点,之后只对虚拟头结点进行操作,这样就不需要分类讨论了。在删除操作都完成后,再回来删去虚拟头结点。

注意的点:注意最后需要head=dummyhead->next;因为原先的头结点可能已经被删去,所以需要重新确定头结点位置。 

代码:

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

LeetCode707. 设计链表

题目链接:707. 设计链表 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:帮你把链表操作学个通透!LeetCode:707.设计链表_哔哩哔哩_bilibili

思路:这是一道全面考察对链表掌握的题目,从构建链表开始,完成以下几个函数,1.初始化链表,2.获得第n个节点的数值,3.头部插入节点,4.尾部插入节点,5.第n个节点前插入,6.删除第n个节点。主要需要创立一个虚拟头结点,剩下的都好办了。

注意的点:头结点的index是0哦。

代码:

class MyLinkedList {
public:
    struct LinkedNode {
        int val;
        LinkedNode* next;
        LinkedNode(int val):val(val), next(nullptr){}
    };

    MyLinkedList() {
        dummyhead= new LinkedNode(0);
        size=0;
    }
    
    int get(int index) {
        if(index<0||index>(size-1)){
            return -1;
        }
        else{
            LinkedNode *cur=dummyhead->next;
            while(index--){
                cur=cur->next;
            }
            return cur->val;
        }
    }
    
    void addAtHead(int val) {
        LinkedNode *newNode=new LinkedNode(val);
        newNode->next=dummyhead->next;
        dummyhead->next=newNode;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode *newNode=new LinkedNode(val);
        LinkedNode *cur=dummyhead;
        while(cur->next!=NULL){
            cur=cur->next;
        }
        cur->next=newNode;
        newNode->next=NULL;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index>size){
            return;
        }
        if(index<0){
            index=0;
        }
        LinkedNode *newNode=new LinkedNode(val);
        LinkedNode *cur=dummyhead;
        while(index--){
            cur=cur->next;
        }
        newNode->next=cur->next;
        cur->next=newNode;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if(index<0||index>(size-1)){
            return;
        }
        LinkedNode *cur=dummyhead;
        while(index--){
            cur=cur->next;
        }
        LinkedNode *tmp=cur->next;
        cur->next=cur->next->next;
        delete tmp;
        size--;
    }
private:
    int size;
    LinkedNode *dummyhead;
};

 LeetCode206. 反转链表

题目链接:206. 反转链表 - 力扣(LeetCode)
文章讲解:代码随想录
视频讲解:帮你拿下反转链表 | LeetCode:206.反转链表 | 双指针法 | 递归法_哔哩哔哩_bilibili

思路1:需要三个指针一个是当前节点的cur指针,一个是上一个节点的pre指针,还有一个是在向后遍历的过程中为了保存之后的节点不要丢失的tmp指针,在向后遍历的过程中,首先保存下一个节点给tmp指针,再将当前cur节点的next指向上一个节点pre,然后再让pre变为cur,cur变为tmp。
在遍历完成后已经完成了翻转的操作,此时pre指向的是头结点,cur指向的是null,所以最后return pre;即可。

代码:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* tmp;
        ListNode *cur=head;
        ListNode *pre=nullptr;
        while(cur!=NULL){
            tmp=cur->next;
            cur->next=pre;
            pre=cur;
            cur=tmp;
        }
        return pre;
    }
};

思路2:递归写法,其实思想跟刚刚那个做法一致,只不过要注意一下递归的写法

代码

class Solution {
public:
    ListNode* reverse(ListNode* pre,ListNode* cur){
        //和前面那个方法的终止条件一样
        if(cur==NULL){
            return pre;
        }
        ListNode* tmp=cur->next;
        cur->next=pre;
        return reverse(cur,tmp);
    }
    ListNode* reverseList(ListNode* head) {
        return reverse(NULL,head);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值