当前位置:   article > 正文

代码随想录训练营 Day 03:LeetCode 203 707

代码随想录训练营 Day 03:LeetCode 203 707

代码随想录训练营 Day 03:LeetCode 203 707

203. 移除链表元素

题目

在这里插入图片描述

解题

分为两步:
更新头结点;
更新非头结点

code

/**
 * 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* removeElements(ListNode* head, int val) {

        while(head != NULL && head->val == val){
            ListNode* tmp = head;
            head = head->next;
            delete tmp;
        }

        ListNode* node = head;
        while(node != NULL && node->next != NULL)
        {
            if(node->next->val == val)
            {
                ListNode* tmp = node->next;
                node->next = node->next->next;
                delete tmp;                
            }
            else{
                node = node->next;
            }                            
        }

        return head;
    }
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

总结

707. 设计链表

题目

在这里插入图片描述

解题

此题目在于考察链表的基本操作,需要熟练掌握

code

class MyLinkedList {
public:

    struct LinkNode{
        int val;
        LinkNode* next;
        LinkNode(int val): val(val), next(NULL){}
    };

    MyLinkedList() {
        _dummyHead = new LinkNode(0);   //虚拟头结点
        _size = 0;
    }
    
    int get(int index) {
        int result = -1;
        if(index < _size && index >= 0)
        {
            LinkNode* cur = _dummyHead->next;
            //for(int i = 0; i <= index; i++)
            while(index--)
            {
                cur = cur->next;
            }
            result = cur->val;
        }
        return result;
    }
    
    void addAtHead(int val) {
        LinkNode* oldhead =_dummyHead->next;
        LinkNode* newhead = new LinkNode(val);
        _dummyHead->next = newhead;
        newhead->next = oldhead;
        _size++;
    }
    
    void addAtTail(int val) {
        LinkNode* endnode =_dummyHead;
        while(endnode->next != NULL)
        {
            endnode = endnode->next;
        }
        LinkNode* newnode = new LinkNode(val);
        endnode->next = newnode;
        newnode->next = NULL;
        _size++;
    }
    
    void addAtIndex(int index, int val) {
        index = index < 0 ? 0 : index;
        if(index == _size )
        {
            //addAtTail(val);
            LinkNode* endnode =_dummyHead;
            while(endnode->next != NULL)
            {
                endnode = endnode->next;
            }
            LinkNode* newnode = new LinkNode(val);
            endnode->next = newnode;
            newnode->next = NULL;
            _size++;
        }
        else if(index < _size )
        {
            LinkNode* curnode =_dummyHead;
            int i = 0;
            while(i < index)
            {
                curnode = curnode->next;
                i++;
            }
            LinkNode* curnode2 = curnode->next;
            LinkNode* newnode = new LinkNode(val);
            curnode->next = newnode;
            newnode->next = curnode2;
            _size++;
        }

    }
    
    void deleteAtIndex(int index) {
        if(index > -1 && index < _size)
        {
            LinkNode* curnode =_dummyHead;
            int i = 0;
            while(i < index )
            {
                curnode = curnode->next;
                i++;
            }
            LinkNode* tmp = curnode->next;
            if(index == _size - 1)
            {
                curnode->next = NULL;
            }
            else
            {
                curnode->next = tmp->next;
            }
            
            //curnode->next = tmp->next;
            delete tmp;
            tmp = NULL;
            _size--;
        }
    }

private:
// 单链表的私有成员变量:(虚拟头结点)、链表长度
// 其余的结点可通过头结点追寻
    LinkNode* _dummyHead;   //
    int _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);
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

总结

对链表的基本构造不太熟悉,注意多多复习

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/680422
推荐阅读
相关标签
  

闽ICP备14008679号