[LeetCode] Design Linked List

707. Design Linked List

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.

A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.

If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked
list are 0-indexed.

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  • void addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.
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
class MyLinkedList {
struct Node {
int val;
Node* next;
};
private:
Node* head;
Node* tail;

private:
Node* getNode(int index) {
Node* cur = head;
for(int i = 0; i < index - 1; i++) {
cur = cur->next;
if(cur == tail)
return cur;
}

return cur;
}
public:
/** Initialize your data structure here. */
MyLinkedList() {
this->head = new Node{-1, nullptr};
tail = head;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
Node* cur = getNode(index);

return index == 0 ? head->val : cur == tail || cur->next == tail ? -1 : cur->next->val;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void addAtHead(int val) {
this->head = new Node{val, this->head};
return ;
}

/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
Node* newTail = new Node{-1, nullptr};
this->tail->val = val;
this->tail->next = newTail;
this->tail = newTail;
return ;
}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(index == 0) {
addAtHead(val);
} else {
Node *cur = getNode(index);

if (cur != tail) {
Node *newNode = new Node{val, cur->next};
cur->next = newNode;
}
}

return ;
}

/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
Node* cur = getNode(index);

if(index == 0) {
head = head->next;
} else if(cur != tail && cur->next != tail) {
cur->next = cur->next->next;
}

return ;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/01/20/PS/LeetCode/design-linked-list/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.