You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Design your implementation of the linked list. You can choose to use the singly linked list or the 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.
8
+
9
+
Implement these functions in your linked list class:
10
+
11
+
-`get(index)` : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
12
+
-`addAtHead(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.
13
+
-`addAtTail(val)` : Append a node of value val to the last element of the linked list.
14
+
-`addAtIndex(index, val)` : 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.
15
+
-`deleteAtIndex(index)` : Delete the index-th node in the linked list, if the index is valid.
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
21
+
"""
22
+
ifindex<0:
23
+
return-1
24
+
it=self.head
25
+
foriinrange(index):
26
+
it=it.next
27
+
ifitisNone:
28
+
return-1
29
+
returnit.val
30
+
31
+
32
+
33
+
defaddAtHead(self, val: int) ->None:
34
+
"""
35
+
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.
36
+
"""
37
+
ifself.headisnotNone:
38
+
temp=self.head
39
+
self.head=Node(val, next=temp)
40
+
temp.prev=self.head
41
+
else:
42
+
self.head=Node(val)
43
+
self.tail=self.head
44
+
self.length+=1
45
+
46
+
47
+
defaddAtTail(self, val: int) ->None:
48
+
"""
49
+
Append a node of value val to the last element of the linked list.
50
+
"""
51
+
ifself.tailisnotNone:
52
+
self.tail.next=Node(val, prev=self.tail)
53
+
self.tail=self.tail.next
54
+
else:
55
+
self.tail=Node(val)
56
+
self.head=self.tail
57
+
self.length+=1
58
+
59
+
60
+
defaddAtIndex(self, index: int, val: int) ->None:
61
+
"""
62
+
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.
63
+
"""
64
+
ifindex>self.length:
65
+
return
66
+
ifindex==0:
67
+
self.addAtHead(val)
68
+
return
69
+
ifindex==self.length:
70
+
self.addAtTail(val)
71
+
return
72
+
it=self.head
73
+
foriinrange(index):
74
+
it=it.next
75
+
newNode=Node(val, prev=it.prev, next=it)
76
+
it.prev.next=newNode
77
+
it.prev=newNode
78
+
self.length+=1
79
+
80
+
81
+
defdeleteAtIndex(self, index: int) ->None:
82
+
"""
83
+
Delete the index-th node in the linked list, if the index is valid.
84
+
"""
85
+
ifindex<0orindex>=self.length:
86
+
return
87
+
ifindex==0:
88
+
self.head=self.head.next
89
+
ifself.headisnotNone:
90
+
self.head.prev=None
91
+
elifindex==self.length-1:
92
+
self.tail=self.tail.prev
93
+
ifself.tailisnotNone:
94
+
self.tail.next=None
95
+
else:
96
+
it=self.head
97
+
foriinrange(index):
98
+
it=it.next
99
+
it.prev.next=it.next
100
+
it.next.prev=it.prev
101
+
self.length-=1
102
+
103
+
104
+
def__str__(self):
105
+
output=""
106
+
it=self.head
107
+
whileitisnotNone:
108
+
output+=str(it.val)
109
+
ifit.nextisnotNone:
110
+
output+=" -> "
111
+
it=it.next
112
+
returnoutput
113
+
114
+
115
+
116
+
# Your MyLinkedList object will be instantiated and called as such:
0 commit comments