Skip to content

Commit a576f3e

Browse files
committed
added Design LinkedList (medium)
1 parent 19ca95f commit a576f3e

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

Medium/DesignLinkedList/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
# Design Linked List
3+
[Leetcode Link](https://leetcode.com/problems/design-linked-list/)
4+
5+
## Problem:
6+
7+
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.
16+
17+
## Example:
18+
19+
```
20+
Input:
21+
["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
22+
[[],[1],[3],[1,2],[1],[1],[1]]
23+
Output:
24+
[null,null,null,null,2,null,3]
25+
26+
Explanation:
27+
MyLinkedList linkedList = new MyLinkedList(); // Initialize empty LinkedList
28+
linkedList.addAtHead(1);
29+
linkedList.addAtTail(3);
30+
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
31+
linkedList.get(1); // returns 2
32+
linkedList.deleteAtIndex(1); // now the linked list is 1->3
33+
linkedList.get(1); // returns 3
34+
```
35+
36+
## Note:
37+
38+
- `0 <= index,val <= 1000`
39+
- Please do not use the built-in LinkedList library.
40+
- At most `2000` calls will be made to `get`, `addAtHead`, `addAtTail`, `addAtIndex` and `deleteAtIndex`.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
class Node:
2+
def __init__(self, val=None, prev=None, next=None):
3+
self.val = val
4+
self.prev = prev
5+
self.next = next
6+
7+
class MyLinkedList:
8+
def __init__(self):
9+
"""
10+
Initialize your data structure here.
11+
"""
12+
self.head = None
13+
self.tail = None
14+
self.length = 0
15+
16+
17+
18+
def get(self, index: int) -> int:
19+
"""
20+
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
21+
"""
22+
if index < 0:
23+
return -1
24+
it = self.head
25+
for i in range(index):
26+
it = it.next
27+
if it is None:
28+
return -1
29+
return it.val
30+
31+
32+
33+
def addAtHead(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+
if self.head is not None:
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+
def addAtTail(self, val: int) -> None:
48+
"""
49+
Append a node of value val to the last element of the linked list.
50+
"""
51+
if self.tail is not None:
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+
def addAtIndex(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+
if index > self.length:
65+
return
66+
if index == 0:
67+
self.addAtHead(val)
68+
return
69+
if index == self.length:
70+
self.addAtTail(val)
71+
return
72+
it = self.head
73+
for i in range(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+
def deleteAtIndex(self, index: int) -> None:
82+
"""
83+
Delete the index-th node in the linked list, if the index is valid.
84+
"""
85+
if index < 0 or index >= self.length:
86+
return
87+
if index == 0:
88+
self.head = self.head.next
89+
if self.head is not None:
90+
self.head.prev = None
91+
elif index == self.length-1:
92+
self.tail = self.tail.prev
93+
if self.tail is not None:
94+
self.tail.next = None
95+
else:
96+
it = self.head
97+
for i in range(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+
while it is not None:
108+
output += str(it.val)
109+
if it.next is not None:
110+
output += " -> "
111+
it = it.next
112+
return output
113+
114+
115+
116+
# Your MyLinkedList object will be instantiated and called as such:
117+
# obj = MyLinkedList()
118+
# param_1 = obj.get(index)
119+
# obj.addAtHead(val)
120+
# obj.addAtTail(val)
121+
# obj.addAtIndex(index,val)
122+
# obj.deleteAtIndex(index)
123+
124+
# test driver
125+
126+
linkedList = MyLinkedList()
127+
linkedList.addAtHead(1)
128+
# linkedList.deleteAtIndex(0)
129+
print(linkedList)
130+
print("GET INDEX 2:", linkedList.get(0))
131+
print("GET HEAD:", str(linkedList.head.val))
132+
print("GET TAIL:", str(linkedList.tail.val))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Languages used: Java and Python
7676
- [Maximum Swap](Medium/MaximumSwap)
7777
- [Number of Operations to Make Network Connected](Medium/NumToMakeNetworkConnected)
7878
- [Sort Colors](Medium/SortColors)
79+
- [Design Linked List](Medium/DesignLinkedList)
7980
- Hard
8081
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
8182
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)