Skip to content

Commit 63d9fee

Browse files
linked list implementation
1 parent 7633ef8 commit 63d9fee

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

Linked Lists.ipynb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Singly Linked Lists\n",
8+
"It is a collection of nodes. Each node has an element and a reference to the next node in the collections. **HEAD** is a member that points to the first node in the collection. The last node is called **TAIL**. The last node is has a reference to *None*. Traversing a linked list is also called **link hopping** or **pointer hopping**.\n",
9+
"\n",
10+
"An important property of a linked list is it does not have a predefined fixed size.\n",
11+
"To insert an element to the HEAD:\n",
12+
"* create a new node with the element\n",
13+
"* new node point to head reference\n",
14+
"* head point to ne node\n",
15+
"\n",
16+
"To insert an element to the TAIL:\n",
17+
"* create a new node\n",
18+
"* tail reference points to new node\n",
19+
"* tail points to new node\n",
20+
"\n",
21+
"Removing an element from HEAD:\n",
22+
"* head points to head reference\n",
23+
"\n",
24+
"Removing an element from TAIL: not an easy task\n",
25+
"\n",
26+
"O(n) -> accessing element\n",
27+
"O(1) -> insertion and deletion of an element "
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"1\n",
40+
"<__main__.Node object at 0x10c6005c0>\n",
41+
"2\n"
42+
]
43+
}
44+
],
45+
"source": [
46+
"class Node(object):\n",
47+
" def __init__(self, ele):\n",
48+
" self.value = ele\n",
49+
" self.next_node = None\n",
50+
" \n",
51+
"a = Node(1)\n",
52+
"b = Node(2)\n",
53+
"c = Node(3)\n",
54+
"\n",
55+
"a.next_node = b\n",
56+
"b.next_node = c\n",
57+
"\n",
58+
"print(a.value)\n",
59+
"print(a.next_node)\n",
60+
"print(a.next_node.value)"
61+
]
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
66+
"source": [
67+
"## Doubly Linked List\n",
68+
"A linked list in which each node keeps a reference to the node after it as well as node before it. The *next* is used to refer to next node and *prev* to refer to previous node.\n",
69+
"\n",
70+
"A **header** node is at the beginning and a **trailer** is node is at the last. These nodes are called sentinel or dummy nodes."
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 5,
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"1\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"class DoublyLLNode(object):\n",
88+
" def __init__(self, ele):\n",
89+
" self.prev_node = None\n",
90+
" self.value = ele\n",
91+
" self.next_node = Node\n",
92+
" \n",
93+
"a = DoublyLLNode(1)\n",
94+
"b = DoublyLLNode(2)\n",
95+
"c = DoublyLLNode(3)\n",
96+
"\n",
97+
"a.next_node = b\n",
98+
"b.prev_node = a\n",
99+
"b.next_node = c\n",
100+
"c.prev_node = b\n",
101+
"\n",
102+
"print(b.prev_node.value)"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"## Interview Questions"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 12,
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"False\n",
122+
"True\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"\"\"\"\n",
128+
"Singly Linked List Cycle List: check if the list has cycles\n",
129+
"\"\"\"\n",
130+
"class Node(object):\n",
131+
" def __init__(self, ele):\n",
132+
" self.value = ele\n",
133+
" self.nextnode = None\n",
134+
" \n",
135+
"def cycle_check(node):\n",
136+
" marker1 = node\n",
137+
" marker2 = node\n",
138+
" while marker2!=None and marker2.nextnode!=None:\n",
139+
" marker1 = marker1.nextnode\n",
140+
" marker2 = marker2.nextnode.nextnode\n",
141+
" if marker2 == marker1:\n",
142+
" return True\n",
143+
" return False\n",
144+
"\n",
145+
"a = Node(1)\n",
146+
"b = Node(2)\n",
147+
"c = Node(3)\n",
148+
"\n",
149+
"a.nextnode = b\n",
150+
"b.nextnode = c\n",
151+
"print(cycle_check(a))\n",
152+
"c.nextnode = a\n",
153+
"print(cycle_check(a))"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": null,
159+
"metadata": {},
160+
"outputs": [],
161+
"source": []
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "Python 3",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.7.3"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 2
185+
}

0 commit comments

Comments
 (0)