Skip to content

Commit 18e2e62

Browse files
Linked Lists
1 parent 0f8daf8 commit 18e2e62

File tree

4 files changed

+476
-0
lines changed

4 files changed

+476
-0
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"n=b"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 5,
15+
"metadata": {},
16+
"outputs": [
17+
{
18+
"name": "stdin",
19+
"output_type": "stream",
20+
"text": [
21+
" 3\n",
22+
" 16\n",
23+
" 13\n",
24+
" 7\n",
25+
" 1\n",
26+
" 2\n"
27+
]
28+
},
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"16 13 1 7\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"a=[]\n",
39+
"for i in range(int(input())):\n",
40+
" a.append(int(input()))\n",
41+
"n1=int(input())\n",
42+
"n2=int(input())\n",
43+
"a.insert(n2,n1)\n",
44+
"print(*(a))"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"# Read, delete-node-at-position"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 82,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"# %load delete-node-at-position.py\n",
61+
"class node:\n",
62+
" def __init__(self,data):\n",
63+
" self.data=data\n",
64+
" self.next=None\n",
65+
"class LinkedList:\n",
66+
" def __init__(self):\n",
67+
" self.start=None\n",
68+
" def display(self):\n",
69+
" if self.start==None:\n",
70+
" print('list is empty')\n",
71+
" else:\n",
72+
" temp=self.start\n",
73+
" while temp:\n",
74+
" print(temp.data)\n",
75+
" temp=temp.next\n",
76+
" def deleteNode(self, position):\n",
77+
" # if linked ist is empty\n",
78+
" temp=self.start\n",
79+
" if temp==None:\n",
80+
" return\n",
81+
" \n",
82+
" # if heads to be removed\n",
83+
" if position==0:\n",
84+
" self.start=temp.next\n",
85+
" temp=None\n",
86+
" return\n",
87+
" \n",
88+
" #iterate nodes for deletion\n",
89+
" for i in range(position-1):\n",
90+
" temp=temp.next\n",
91+
" if temp is None:\n",
92+
" break\n",
93+
" \n",
94+
" #if position>length of linked list\n",
95+
" if temp is None:\n",
96+
" return\n",
97+
" if temp.next is None:\n",
98+
" return\n",
99+
" \n",
100+
" next=temp.next.next\n",
101+
" temp.next=None\n",
102+
" temp.next=next\n",
103+
" \n",
104+
"l=LinkedList()\n",
105+
"for i in range(int(input())):\n",
106+
" n=int(input())\n",
107+
" if i==0:\n",
108+
" l.start=node(n)\n",
109+
" ptr=l.start\n",
110+
" else:\n",
111+
" ptr.next=node(n)\n",
112+
" ptr=ptr.next"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 71,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"def deleteNodeAtPosition(head, position):\n",
122+
" # if linked ist is empty\n",
123+
" temp=head\n",
124+
" if temp==None:\n",
125+
" return\n",
126+
" \n",
127+
" # if heads to be removed\n",
128+
" if position==0:\n",
129+
" head=temp.next\n",
130+
" temp=None\n",
131+
" return\n",
132+
" \n",
133+
" #iterate nodes for deletion\n",
134+
" for i in range(position-1):\n",
135+
" temp=temp.next\n",
136+
" if temp is None:\n",
137+
" break\n",
138+
" \n",
139+
" #if position>length of linked list\n",
140+
" if temp is None:\n",
141+
" return\n",
142+
" if temp.next is None:\n",
143+
" return\n",
144+
" \n",
145+
" next=temp.next.next\n",
146+
" temp.next=None\n",
147+
" temp.next=next"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"# insert at beginning"
155+
]
156+
},
157+
{
158+
"cell_type": "code",
159+
"execution_count": 1,
160+
"metadata": {},
161+
"outputs": [],
162+
"source": [
163+
"# %load insert-at-beginning.py\n",
164+
"class node:\n",
165+
" def __init__(self,data):\n",
166+
" self.data=data\n",
167+
" self.next=None\n",
168+
"class LinkedList:\n",
169+
" def __init__(self):\n",
170+
" self.start=None\n",
171+
" def display(self):\n",
172+
" if self.start==None:\n",
173+
" print('list is empty')\n",
174+
" else:\n",
175+
" temp=self.start\n",
176+
" while temp:\n",
177+
" print(temp.data)\n",
178+
" temp=temp.next\n",
179+
" def InsertAtHead(self,n):\n",
180+
" if self.start is None:\n",
181+
" self.start=node(n)\n",
182+
" else:\n",
183+
" temp=self.start\n",
184+
" self.start=node(n)\n",
185+
" self.start.next=temp\n",
186+
"l=LinkedList()\n",
187+
"for i in range(int(input())):\n",
188+
" n=int(input())\n",
189+
" if i==0:\n",
190+
" l.start=node(n)\n",
191+
" ptr=l.start\n",
192+
" else:\n",
193+
" ptr.next=node(n)\n",
194+
" ptr=ptr.next"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": 77,
200+
"metadata": {},
201+
"outputs": [
202+
{
203+
"name": "stdout",
204+
"output_type": "stream",
205+
"text": [
206+
"12\n"
207+
]
208+
}
209+
],
210+
"source": [
211+
"l.display()"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": 78,
217+
"metadata": {},
218+
"outputs": [],
219+
"source": [
220+
"l.deleteNode(1)"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": 75,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"deleteNodeAtPosition(l.start,1)"
230+
]
231+
},
232+
{
233+
"cell_type": "code",
234+
"execution_count": 80,
235+
"metadata": {},
236+
"outputs": [
237+
{
238+
"data": {
239+
"text/plain": [
240+
"__main__.LinkedList"
241+
]
242+
},
243+
"execution_count": 80,
244+
"metadata": {},
245+
"output_type": "execute_result"
246+
}
247+
],
248+
"source": [
249+
"type(l)"
250+
]
251+
},
252+
{
253+
"cell_type": "markdown",
254+
"metadata": {},
255+
"source": [
256+
"# Print in Reverse"
257+
]
258+
},
259+
{
260+
"cell_type": "code",
261+
"execution_count": 29,
262+
"metadata": {},
263+
"outputs": [
264+
{
265+
"name": "stdin",
266+
"output_type": "stream",
267+
"text": [
268+
" 5\n",
269+
" 1\n",
270+
" 6\n",
271+
" 3\n",
272+
" 8\n",
273+
" 2\n"
274+
]
275+
},
276+
{
277+
"name": "stdout",
278+
"output_type": "stream",
279+
"text": [
280+
"2\n",
281+
"8\n",
282+
"3\n",
283+
"6\n",
284+
"1\n"
285+
]
286+
}
287+
],
288+
"source": [
289+
"# %load print-in-reverse.py\n",
290+
"class node:\n",
291+
" def __init__(self,data):\n",
292+
" self.data=data\n",
293+
" self.next=None\n",
294+
"class LinkedList:\n",
295+
" def __init__(self):\n",
296+
" self.start=None\n",
297+
" def display(self):\n",
298+
" if self.start==None:\n",
299+
" print('list is empty')\n",
300+
" else:\n",
301+
" temp=self.start\n",
302+
" while temp:\n",
303+
" print(temp.data)\n",
304+
" temp=temp.next\n",
305+
" def display_reverse(self,head):\n",
306+
" if head:\n",
307+
" self.display_reverse(head.next)\n",
308+
" print(head.data)\n",
309+
" def InsertAtHead(self,n):\n",
310+
" if self.start is None:\n",
311+
" self.start=node(n)\n",
312+
" else:\n",
313+
" temp=self.start\n",
314+
" self.start=node(n)\n",
315+
" self.start.next=temp\n",
316+
"l=LinkedList()\n",
317+
"for _ in range(int(input())):\n",
318+
" n=int(input())\n",
319+
" if l .start==None:\n",
320+
" l.start=node(n)\n",
321+
" ptr=l.start\n",
322+
" else:\n",
323+
" ptr.next=node(n)\n",
324+
" ptr=ptr.next\n",
325+
"l.display_reverse(l.start)"
326+
]
327+
},
328+
{
329+
"cell_type": "markdown",
330+
"metadata": {},
331+
"source": [
332+
"# Reverse a linked list"
333+
]
334+
}
335+
],
336+
"metadata": {
337+
"kernelspec": {
338+
"display_name": "Python 3",
339+
"language": "python",
340+
"name": "python3"
341+
},
342+
"language_info": {
343+
"codemirror_mode": {
344+
"name": "ipython",
345+
"version": 3
346+
},
347+
"file_extension": ".py",
348+
"mimetype": "text/x-python",
349+
"name": "python",
350+
"nbconvert_exporter": "python",
351+
"pygments_lexer": "ipython3",
352+
"version": "3.8.2"
353+
}
354+
},
355+
"nbformat": 4,
356+
"nbformat_minor": 4
357+
}

0 commit comments

Comments
 (0)