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

203.移除链表元素

代码随想录链接:代码随想录 (programmercarl.com)

1. 在原本的链表上操作

移除头节点一开始写成if了,应该是while才对,有可能一直等于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* tmp=cur->next;
                cur->next=cur->next->next;
                delete tmp;
            }
            else{
                cur=cur->next;
            }
        }
        return head;
    }
};

2. 虚拟头节点

虚拟头节点的定义老是忘了怎么写

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* dummyhead=new ListNode(0);
        dummyhead->next=head;
        ListNode* cur =dummyhead;
        while(cur!=NULL && 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;
    }
};

707.设计链表

代码随想录链接:代码随想录 (programmercarl.com)

一些基本的链表操作,就是老是忘了size加减

class MyLinkedList {
public:
        int size;
    struct LinkedNode{
        int val;
        LinkedNode* next;
        LinkedNode(int x):val(x),next(nullptr){}
    };
    LinkedNode* dummyhead;
    MyLinkedList() {
        dummyhead= new LinkedNode(0);
        size=0;
    }
    
    int get(int index) {
        if(index<0 || index>=size){
            return -1;
        }
        else{
            LinkedNode* cur=dummyhead->next;
            for(int count=0;count<index;count++){
                cur=cur->next;
            }
            return cur->val;
        }
    }
    
    void addAtHead(int val) {
        LinkedNode* tmp=dummyhead->next;
        LinkedNode* node=new LinkedNode(val);
        node->next=tmp;
        dummyhead->next=node;
        size++;
    }
    
    void addAtTail(int val) {
        LinkedNode* node=new LinkedNode(val);
        LinkedNode *cur=dummyhead;
        while(cur!=NULL && cur->next!=NULL){
            cur=cur->next;
        }
        cur->next=node;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index > size) return;
        if(index < 0) index = 0; 
        LinkedNode* cur=dummyhead;
        for(int count=0;count<index;count++){
            cur=cur->next;
        }
        LinkedNode* node = new LinkedNode(val);
        node->next=cur->next;
        cur->next=node;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if(index>=0 && index<size){
            LinkedNode* cur=dummyhead;
            for(int count=0;count<index;count++){
                cur=cur->next;
            }
            LinkedNode* tmp=cur->next;
            cur->next=cur->next->next;
            delete tmp;
            size--;
        }
        else{
            return;
        }
    }
};

206.反转链表 

题解用的双指针,之前写的时候用的是头插,只要遍历链表然后头插一个新的就能反转了。

1. 头插

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

2. 双指针

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

3. 递归 

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

总结

链表第一天,去年冬天在牛客刷了很久的链表,思路还行,就是细节有一些问题。设计链表费了点时间,用时大概3小时

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值