Skip to content

Commit 7dc9561

Browse files
authored
GH-90: Add Python Linked List (#103)
1 parent 10b7de7 commit 7dc9561

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ A data structure is a data organization, management, and storage format that is
5353
<tr>
5454
<td>Linked List</td>
5555
<td>
56+
<a href="/concepts/python/linked-list.md"><code>py</code></a>
5657
<a href="/concepts/typescript/linked-list.md"><code>ts</code></a>
5758
</td>
5859
<tr>

concepts/python/linked_list.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Linked List
2+
3+
From wikipedia
4+
5+
> In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
6+
7+
## Time Complexity Analysis of Linked List
8+
9+
INSERTION
10+
11+
| Operation | Singly Linked List | Double Linked List |
12+
|-----------------|---------------------|---------------------|
13+
| Insert head | $O(1)$ | $O(1)$ |
14+
| Insert middle | $O(n)$ | $O(n)$ |
15+
| Insert tail | $O(n)$ | $O(1)$ |
16+
17+
DELETION
18+
19+
| Operation | Singly Linked List | Double Linked List |
20+
|-----------------|---------------------|---------------------|
21+
| Delete head | $O(1)$ | $O(1)$ |
22+
| Delete middle | $O(n)$ | $O(n)$ |
23+
| Delete tail | $O(n)$ | $O(1)$ |
24+
25+
SEARCHING
26+
27+
| Singly Linked List | Double Linked List |
28+
|---------------------|---------------------|
29+
| $O(n)$ | $O(n)$ |
30+
31+
## Singly Linked List Implementation in Python
32+
33+
```python
34+
# Implement using collections.deque
35+
from collections import deque
36+
37+
38+
linked_list = deque(['a','b','c','d','e']) # a -> b -> c -> d -> e -> None
39+
linked_list.append('f') # a -> b -> c -> d -> e -> f -> None
40+
linked_list.pop() # a -> b -> c -> d -> e -> None
41+
linked_list.appendleft('z') # z -> a -> b -> c -> d -> e -> None
42+
linked_list.popleft() # a -> b -> c -> d -> e -> None
43+
44+
# Implement from scratch
45+
class LinkedList:
46+
def __init__(self, nodes=None):
47+
self.head = None
48+
if nodes is not None:
49+
node = Node(data=nodes.pop(0))
50+
self.head = node
51+
for item in nodes:
52+
node.next = Node(data=item)
53+
node = node.next
54+
55+
def __iter__(self):
56+
node = self.head
57+
while node is not None:
58+
yield node
59+
node = node.next
60+
61+
def __repr__(self):
62+
node = self.head
63+
nodes = []
64+
while node is not None:
65+
nodes.append(node.data)
66+
node = node.next
67+
nodes.append("None")
68+
return " -> ".join(nodes)
69+
70+
def add_first(self, node):
71+
node.next = self.head
72+
self.head = node
73+
74+
def add_last(self, node):
75+
if self.head is None:
76+
self.head = node
77+
return
78+
for current_node in self:
79+
pass
80+
current_node.next = node
81+
82+
class Node:
83+
def __init__(self, data):
84+
self.data = data
85+
self.next = None
86+
87+
def __repr__(self):
88+
return self.data
89+
90+
linked_list = LinkedList(['b','c','d','e']) # b -> c -> d -> e -> None
91+
linked_list.add_first(Node("a")) # a -> b -> c -> d -> e -> None
92+
linked_list.add_last(Node("f")) # a -> b -> c -> d -> e -> f -> None
93+
```
94+
## 🔗 Further Reading
95+
96+
* [Linked Lists in Python](https://realpython.com/linked-lists-python/), realpython.com
97+
* ▶️ [Linked Lists Introduction](https://www.youtube.com/watch?v=-Yn5DU0_-lw&t=7s&ab_channel=WilliamFiset), WilliamFiset, 2017
98+
* ▶️ [CS50 2018 - Lecture 4 - Linked Lists](https://www.youtube.com/watch?v=wh4TS7RJDTA), CS50, 2018

0 commit comments

Comments
 (0)