707. 设计链表

**你可以选择使用单链表或者双链表,设计并实现自己的链表。

单链表中的节点应该具备两个属性:val 和 next 。val 是当前节点的值,next 是指向下一个节点的指针/引用。

如果是双向链表,则还需要属性 prev 以指示链表中的上一个节点。假设链表中的所有节点下标从 0 开始。

实现 MyLinkedList 类:

示例:

输入
[“MyLinkedList”, “addAtHead”, “addAtTail”, “addAtIndex”, “get”, “deleteAtIndex”, “get”]
[[], [1], [3], [1, 2], [1], [1], [1]]
输出
[null, null, null, null, 2, null, 3]

解释
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // 链表变为 1->2->3
myLinkedList.get(1); // 返回 2
myLinkedList.deleteAtIndex(1); // 现在,链表变为 1->3
myLinkedList.get(1); // 返回 3**


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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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 > (_size - 1) || index < 0) return -1;

        LinkedNode *cur = _dummyhead->next;

        while(index --)

        {

            cur = cur ->next;

        }

        return cur->val;

    }

    void addAtHead(int val) {

        LinkedNode *new_node = new LinkedNode(val);

        new_node->next = _dummyhead ->next;

        _dummyhead ->next = new_node;

        _size++;

    }

    void addAtTail(int val) {

        LinkedNode *new_node = new LinkedNode(val);

        LinkedNode *cur = _dummyhead;

        while(cur ->next != nullptr) cur = cur->next;

        cur ->next = new_node;

        _size++;

    }

    void addAtIndex(int index, int val) {

        if(index > _size) return ;

        if(index < 0) index =0;

        LinkedNode *cur = _dummyhead;

        LinkedNode *new_node = new LinkedNode(val);

        while(index --)

        {

            cur = cur->next;

        }

        new_node->next = cur->next;

        cur->next = new_node;

        _size++;

    }

    void deleteAtIndex(int index) {

        if(index >= _size|| index < 0) return ;

        LinkedNode* cur = _dummyhead;

        LinkedNode *tmp;

        while(index --)

        {

            cur=cur->next;

        }

        tmp = cur->next;

        cur->next = cur->next->next;

        delete tmp;

        tmp = nullptr;

        _size--;

    }

private:

    int _size;

    LinkedNode *_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);

 */

上次更新 2025-04-05