代码随想录Day3|LeetCode203 移除链表元素,LeetCode707设计链表,LeetCode206反转链表

题目链接:203.移除链表元素

思路:在首元节点前设置一个头结点,就不需要对首元节点和后面的节点分开做操作了

代码:

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

题目链接: 707.设计链表

代码:

class MyLinkedList {
public:

    struct ListNode{
        int val;
        ListNode* next;
        ListNode(int val):val(val),next(nullptr){};
    };
    int size;
    ListNode* dummyhead;
    MyLinkedList() {
        size=0;
        dummyhead=new ListNode(0);
    }
    
    int get(int index) {
        if(index<0 || index>=size){
            return -1;
        }
        ListNode* cur=dummyhead->next;
        while(index--){
            cur=cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        ListNode* node=new ListNode(val);
        node->next=dummyhead->next;
        dummyhead->next=node;
        size++;
    }
    
    void addAtTail(int val) {
        ListNode* cur=dummyhead;
        while(cur->next){
            cur=cur->next;
        }
        ListNode* node=new ListNode(val);
        node->next=cur->next;
        cur->next=node;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index>size) return;
        if(index<0) index=0;
        ListNode* node = new ListNode(val);
        ListNode* cur=dummyhead;
        while(index--){
            cur=cur->next;
        }
        node->next=cur->next;
        cur->next=node;
        size++;
    }
    
    void deleteAtIndex(int index) {
        if(index>=size||index<0)    return;
        ListNode* cur=dummyhead;
        while(index--){
            cur=cur->next;
        }
        ListNode* tmp=cur->next;
        cur->next=tmp->next;
        delete tmp;
        size--;
    }
};

/**
 * 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);
 */

代码链接 :206.翻转链表

思路:每次遍历一个节点(cur),要让他指向前一个节点(pre),指向前一个节点后本身的指向消失,所以要用tmp存下cur->next

代码:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值