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

203.移动链表元素

题目链接:

https://leetcode.cn/problems/remove-linked-list-elements/

解题方法一:

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        ListNode* cur = head;
        //删除头节点
        for(;head!= NULL && head->val == val;)
        {
            head = head->next;
        }
        //删除非头节点
        for(;cur!= NULL && cur->next != NULL;)
        {
             if(cur ->next-> val == val )
             {
                   cur->next = cur->next->next;    
             }
             else {
                    cur = cur->next;
             }
        }
        return head;
    }
};

解题方法二:设置虚拟链表头节点

涉及增加或删除节点可以使用该方法

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
       ListNode* tempNode = (ListNode*)malloc(sizeof(ListNode));
       tempNode->next = head;
       tempNode->val = 0;
       ListNode* cur = tempNode;
        //删除非头节点
        for(;cur!= NULL && cur->next != NULL;)
        {
             if(cur ->next-> val == val )
             {
                   cur->next = cur->next->next;    
             }
             else {
                    cur = cur->next;
             }
        }
        head = tempNode->next;
        free(tempNode);
        return head;
    }
};

707.设计链表

题目链接

https://leetcode.cn/problems/design-linked-list/

实现链表:获取链表指定节点的值、在链表头节点或尾节点插入新节点、在指定位置添加新节点、删除指定节点

单链表:

解题方法一:

这个代码执行不通过

提示:

class MyLinkedList {
public:
    struct LinkedList{
        int val;
        LinkedList * next;
    };

    MyLinkedList() {
        _dummyHead = (LinkedList *)malloc(sizeof(LinkedList));
        _dummyHead->next = NULL;
        _dummyHead->val = 0;
        _size = 0;
    }
    
    int get(int index) {
        if(index > _size - 1 || index < 0)
        {
            return -1;
        }
        LinkedList * cur = _dummyHead->next;
        while(index --) // --index和index区别
        {
            cur = cur->next;
        }
       return cur->val;
        
    }
    
    void addAtHead(int val) {
        LinkedList * Head = (LinkedList *)malloc(sizeof(LinkedList));
        Head->next = _dummyHead;
        Head->val = val;
        _size++;
    }
    
    void addAtTail(int val) {
         LinkedList * cur = _dummyHead;
         while(cur->next != NULL)
        {
            cur = cur->next;
        }
        LinkedList *tail = (LinkedList *)malloc(sizeof(LinkedList));
        tail->next = NULL;
        tail->val = val;
        cur->next =  tail;
        _size++;
    }
    
    void addAtIndex(int index, int val) {

        if(index > _size - 1 || index < 0)
        {
            //return -1;
        }
        if(index == 0)
        {
            LinkedList *Head = (LinkedList *)malloc(sizeof(LinkedList));
            Head->next = _dummyHead;
            Head->val = val;
            _size++;
        }
        else
        {
            LinkedList * cur = _dummyHead;
            while(index--)
            {
                cur = cur->next;
            }
            LinkedList *temp = (LinkedList *)malloc(sizeof(LinkedList));
            temp->next = cur->next;
            cur->next = temp;
            temp->val = val;
            _size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if(index > _size - 1 || index < 0)
        {
            //return -1;
        }
        if(index == 0)
        {
            _dummyHead = _dummyHead->next;
            _size--;
        }
        else
        {
           LinkedList * cur = _dummyHead;
           while(index --)
           {
             cur = cur->next;
           }
           cur->next = cur->next->next;
           _size--;
        }  
    }
private:
    int _size;
    LinkedList* _dummyHead;
};

/**
 * 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);
 */
class MyLinkedList {
public:
    struct LinkedList{
        int val;
        LinkedList * next;
    };

    MyLinkedList() {
        _dummyHead = (LinkedList *)malloc(sizeof(LinkedList));
        _dummyHead->next = NULL;
        _dummyHead->val = 0;
        _size = 0;
    }
    
    int get(int index) {
        if(index > (_size - 1) || index < 0)
        {
            return -1;
        }
       LinkedList* cur = _dummyHead->next;
        while(cur->next != NULL && index--){ // 如果--index 就会陷入死循环
            cur = cur->next;
        }
        return cur->val;
    }
    
    void addAtHead(int val) {
        LinkedList * Head = (LinkedList *)malloc(sizeof(LinkedList));
        Head->next = _dummyHead;
        Head->val = val;
        _size++;
    }
    
    void addAtTail(int val) {
         LinkedList * cur = _dummyHead;
         while(cur->next != NULL)
        {
            cur = cur->next;
        }
        LinkedList *tail = (LinkedList *)malloc(sizeof(LinkedList));
        tail->next = NULL;
        tail->val = val;
        cur->next =  tail;
        _size++;
    }
    
    void addAtIndex(int index, int val) {

        if(index > _size - 1 || index < 0)
        {
            //return -1;
        }
        if(index == 0)
        {
            LinkedList *Head = (LinkedList *)malloc(sizeof(LinkedList));
            Head->next = _dummyHead;
            Head->val = val;
            _size++;
        }
        else
        {
            LinkedList * cur = _dummyHead;
            while(index--)
            {
                cur = cur->next;
            }
            LinkedList *temp = (LinkedList *)malloc(sizeof(LinkedList));
            temp->next = cur->next;
            cur->next = temp;
            temp->val = val;
            _size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if(index > _size - 1 || index < 0)
        {
            //return -1;
        }
        if(index == 0)
        {
            _dummyHead = _dummyHead->next;
            _size--;
        }
        else
        {
           LinkedList * cur = _dummyHead;
           while(index --)
           {
             cur = cur->next;
           }
           cur->next = cur->next->next;
           _size--;
        }  
    }
private:
    int _size;
    LinkedList* _dummyHead;
};

/**
 * 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.反转链表

https://leetcode.cn/problems/reverse-linked-list/

解题方法一:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {

        ListNode* temp; 
        ListNode* cur = head;
        ListNode* pre = NULL;
        while(cur) {
            temp = cur->next;  
            cur->next = pre; 
            pre = cur;
            cur = temp;
        }
        return pre;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值