Skip to content

Commit 3bd4fcb

Browse files
insert at beginning of singly linked list
1 parent 36a9888 commit 3bd4fcb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 InsertAtHead(self,n):
17+
if self.start is None:
18+
self.start=node(n)
19+
else:
20+
temp=self.start
21+
self.start=node(n)
22+
self.start.next=temp
23+
l=LinkedList()
24+
for i in range(int(input())):
25+
n=int(input())
26+
if i==0:
27+
l.start=node(n)
28+
ptr=l.start
29+
else:
30+
ptr.next=node(n)
31+
ptr=ptr.next

0 commit comments

Comments
 (0)