Skip to content

Commit b9754fd

Browse files
read and delete node at given postion in linked list
1 parent 20f2e70 commit b9754fd

File tree

2 files changed

+279
-0
lines changed

2 files changed

+279
-0
lines changed
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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": "code",
49+
"execution_count": 82,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"# %load delete-node-at-position.py\n",
54+
"class node:\n",
55+
" def __init__(self,data):\n",
56+
" self.data=data\n",
57+
" self.next=None\n",
58+
"class LinkedList:\n",
59+
" def __init__(self):\n",
60+
" self.start=None\n",
61+
" def display(self):\n",
62+
" if self.start==None:\n",
63+
" print('list is empty')\n",
64+
" else:\n",
65+
" temp=self.start\n",
66+
" while temp:\n",
67+
" print(temp.data)\n",
68+
" temp=temp.next\n",
69+
" def deleteNode(self, position):\n",
70+
" # if linked ist is empty\n",
71+
" temp=self.start\n",
72+
" if temp==None:\n",
73+
" return\n",
74+
" \n",
75+
" # if heads to be removed\n",
76+
" if position==0:\n",
77+
" self.start=temp.next\n",
78+
" temp=None\n",
79+
" return\n",
80+
" \n",
81+
" #iterate nodes for deletion\n",
82+
" for i in range(position-1):\n",
83+
" temp=temp.next\n",
84+
" if temp is None:\n",
85+
" break\n",
86+
" \n",
87+
" #if position>length of linked list\n",
88+
" if temp is None:\n",
89+
" return\n",
90+
" if temp.next is None:\n",
91+
" return\n",
92+
" \n",
93+
" next=temp.next.next\n",
94+
" temp.next=None\n",
95+
" temp.next=next\n",
96+
" \n",
97+
"l=LinkedList()\n",
98+
"for i in range(int(input())):\n",
99+
" n=int(input())\n",
100+
" if i==0:\n",
101+
" l.start=node(n)\n",
102+
" ptr=l.start\n",
103+
" else:\n",
104+
" ptr.next=node(n)\n",
105+
" ptr=ptr.next"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 71,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"def deleteNodeAtPosition(head, position):\n",
115+
" # if linked ist is empty\n",
116+
" temp=head\n",
117+
" if temp==None:\n",
118+
" return\n",
119+
" \n",
120+
" # if heads to be removed\n",
121+
" if position==0:\n",
122+
" head=temp.next\n",
123+
" temp=None\n",
124+
" return\n",
125+
" \n",
126+
" #iterate nodes for deletion\n",
127+
" for i in range(position-1):\n",
128+
" temp=temp.next\n",
129+
" if temp is None:\n",
130+
" break\n",
131+
" \n",
132+
" #if position>length of linked list\n",
133+
" if temp is None:\n",
134+
" return\n",
135+
" if temp.next is None:\n",
136+
" return\n",
137+
" \n",
138+
" next=temp.next.next\n",
139+
" temp.next=None\n",
140+
" temp.next=next"
141+
]
142+
},
143+
{
144+
"cell_type": "code",
145+
"execution_count": 77,
146+
"metadata": {},
147+
"outputs": [
148+
{
149+
"name": "stdout",
150+
"output_type": "stream",
151+
"text": [
152+
"12\n"
153+
]
154+
}
155+
],
156+
"source": [
157+
"l.display()"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 78,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": [
166+
"l.deleteNode(1)"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": 75,
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"deleteNodeAtPosition(l.start,1)"
176+
]
177+
},
178+
{
179+
"cell_type": "code",
180+
"execution_count": 80,
181+
"metadata": {},
182+
"outputs": [
183+
{
184+
"data": {
185+
"text/plain": [
186+
"__main__.LinkedList"
187+
]
188+
},
189+
"execution_count": 80,
190+
"metadata": {},
191+
"output_type": "execute_result"
192+
}
193+
],
194+
"source": [
195+
"type(l)"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": null,
201+
"metadata": {},
202+
"outputs": [],
203+
"source": []
204+
}
205+
],
206+
"metadata": {
207+
"kernelspec": {
208+
"display_name": "Python 3",
209+
"language": "python",
210+
"name": "python3"
211+
},
212+
"language_info": {
213+
"codemirror_mode": {
214+
"name": "ipython",
215+
"version": 3
216+
},
217+
"file_extension": ".py",
218+
"mimetype": "text/x-python",
219+
"name": "python",
220+
"nbconvert_exporter": "python",
221+
"pygments_lexer": "ipython3",
222+
"version": "3.8.2"
223+
}
224+
},
225+
"nbformat": 4,
226+
"nbformat_minor": 4
227+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class node:
2+
def __init__(self,data):
3+
self.data=data
4+
self.next=None
5+
class LinkedList:
6+
def __init__(self):
7+
self.start=None
8+
def display(self):
9+
if self.start==None:
10+
print('list is empty')
11+
else:
12+
temp=self.start
13+
while temp:
14+
print(temp.data)
15+
temp=temp.next
16+
def deleteNode(self, position):
17+
# if linked ist is empty
18+
temp=self.start
19+
if temp==None:
20+
return
21+
22+
# if heads to be removed
23+
if position==0:
24+
self.start=temp.next
25+
temp=None
26+
return
27+
28+
#iterate nodes for deletion
29+
for i in range(position-1):
30+
temp=temp.next
31+
if temp is None:
32+
break
33+
34+
#if position>length of linked list
35+
if temp is None:
36+
return
37+
if temp.next is None:
38+
return
39+
40+
next=temp.next.next
41+
temp.next=None
42+
temp.next=next
43+
44+
l=LinkedList()
45+
for i in range(int(input())):
46+
n=int(input())
47+
if i==0:
48+
l.start=node(n)
49+
ptr=l.start
50+
else:
51+
ptr.next=node(n)
52+
ptr=ptr.next

0 commit comments

Comments
 (0)