-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign Linked List.cpp
More file actions
161 lines (142 loc) · 3.96 KB
/
Design Linked List.cpp
File metadata and controls
161 lines (142 loc) · 3.96 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class MyLinkedList {
private:
//create a node structure
struct Node
{
int val;
Node* next;
};
Node* head;
int length;
public:
//initialize head and length
MyLinkedList() {
head = NULL;
length=0;
}
int get(int index) {
//traverse through list until i == index or until end of list and return the result
Node* temp = head;
int i = 0;
while(temp!=NULL)
{
if(i == index)
return temp->val;
i++;
temp = temp->next;
}
return -1;
}
void addAtHead(int val) {
//add node to head, if list is empty just assign head to the node
if(head == NULL)
{
head = new Node;
head->val = val;
head->next = NULL;
}
else
{
Node* temp = new Node;
temp->val = val;
temp->next = head;
head = temp;
}
//increment length
length++;
}
void addAtTail(int val) {
//add node at the end of list, if list is empty, then assign head to new node
if(head == NULL)
{
head = new Node;
head->val = val;
head->next = NULL;
}
else
{
Node* temp = head;
while(temp->next != NULL)
temp=temp->next;
Node* temp2 = new Node;
temp2->val = val;
temp2->next = NULL;
temp->next = temp2;
}
//increment length
length++;
}
void addAtIndex(int index, int val) {
//add node to specific index, if index == length then add node at tail
if(index == length)
addAtTail(val);
//else add node at the given index
else if(index < length)
{
//if index == 0 then add node at head
if(index == 0)
addAtHead(val);
//else use two pointers to put node at given index
else
{
int i=1;
Node *p1=head, *p2 = head->next;
while(i!=index)
{
p1=p2;
p2=p2->next;
i++;
}
Node* temp = new Node;
temp->val = val;
temp->next = p2;
p1->next = temp;
//increment length
length++;
}
}
}
void deleteAtIndex(int index) {
//delete node, if list is empty or index is invalid, then tehre is nothing to delete
if(head == NULL || index >= length)
return;
//else delete a node at given index
else
{
//if there is only one node, then just assign head to null
if(head->next == NULL)
head = NULL;
//else delete specific node
else
{
//if index i== 0, move the head pointer one step forward
if(index == 0)
head = head->next;
//else, use two pointers to delete the node
else
{
int i=1;
Node *p1=head, *p2=head->next;
while(i!=index)
{
p1=p2;
p2=p2->next;
i++;
}
p1->next = p2->next;
}
}
//decrement length
length--;
}
}
};
/**
* 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);
*/