Skip to content

Commit 628cc36

Browse files
authored
Merge pull request nirmalnishant645#18 from nirmalnishant645/problem
Design Linked List
2 parents 043e3da + 9818f0c commit 628cc36

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

0707-Design-Linked_list.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
'''
2+
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.
3+
4+
Implement these functions in your linked list class:
5+
6+
get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
7+
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.
8+
addAtTail(val) : Append a node of value val to the last element of the linked list.
9+
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.
10+
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.
11+
12+
13+
Example:
14+
15+
Input:
16+
["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
17+
[[],[1],[3],[1,2],[1],[1],[1]]
18+
Output:
19+
[null,null,null,null,2,null,3]
20+
21+
Explanation:
22+
MyLinkedList linkedList = new MyLinkedList(); // Initialize empty LinkedList
23+
linkedList.addAtHead(1);
24+
linkedList.addAtTail(3);
25+
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
26+
linkedList.get(1); // returns 2
27+
linkedList.deleteAtIndex(1); // now the linked list is 1->3
28+
linkedList.get(1); // returns 3
29+
30+
31+
Constraints:
32+
33+
0 <= index,val <= 1000
34+
Please do not use the built-in LinkedList library.
35+
At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.
36+
'''
37+
class Node:
38+
def __init__(self, data=None):
39+
self.data = data
40+
self.next = None
41+
42+
class MyLinkedList:
43+
44+
def __init__(self):
45+
"""
46+
Initialize your data structure here.
47+
"""
48+
self.head = None
49+
self.tail = None
50+
self.size = 0
51+
52+
def get(self, index: int) -> int:
53+
"""
54+
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
55+
"""
56+
if index >= self.size:
57+
return -1
58+
cur_node = self.head
59+
for _ in range(index):
60+
cur_node = cur_node.next
61+
return cur_node.data
62+
63+
64+
def addAtHead(self, val: int) -> None:
65+
"""
66+
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.
67+
"""
68+
temp = Node(val)
69+
if not self.head:
70+
self.head = temp
71+
self.tail = self.head
72+
else:
73+
temp.next = self.head
74+
self.head = temp
75+
self.size += 1
76+
77+
78+
def addAtTail(self, val: int) -> None:
79+
"""
80+
Append a node of value val to the last element of the linked list.
81+
"""
82+
temp = Node(val)
83+
if not self.tail:
84+
self.tail = temp
85+
self.head = self.tail
86+
else:
87+
self.tail.next = temp
88+
self.tail = temp
89+
self.size += 1
90+
91+
92+
def addAtIndex(self, index: int, val: int) -> None:
93+
"""
94+
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.
95+
"""
96+
if index > self.size:
97+
return
98+
if index == 0:
99+
self.addAtHead(val)
100+
elif index == self.size:
101+
self.addAtTail(val)
102+
else:
103+
temp = Node(val)
104+
cur_node = self.head
105+
for _ in range(index - 1):
106+
cur_node = cur_node.next
107+
temp.next = cur_node.next
108+
cur_node.next = temp
109+
self.size += 1
110+
111+
112+
def deleteAtIndex(self, index: int) -> None:
113+
"""
114+
Delete the index-th node in the linked list, if the index is valid.
115+
"""
116+
if index >= self.size:
117+
return
118+
elif index == 0:
119+
self.head = self.head.next
120+
self.size -= 1
121+
else:
122+
cur_node = self.head
123+
for _ in range(index - 1):
124+
cur_node = cur_node.next
125+
cur_node.next = cur_node.next.next
126+
if index == self.size - 1:
127+
self.tail = cur_node
128+
self.size -= 1
129+
130+
131+
# Your MyLinkedList object will be instantiated and called as such:
132+
# obj = MyLinkedList()
133+
# param_1 = obj.get(index)
134+
# obj.addAtHead(val)
135+
# obj.addAtTail(val)
136+
# obj.addAtIndex(index,val)
137+
# obj.deleteAtIndex(index)

0 commit comments

Comments
 (0)