Skip to content

Commit 32bef8c

Browse files
authored
Create Insert_a_node_at_tail_of_linked_list.py
1 parent 4a4657f commit 32bef8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Insert Node at the end of a linked list
3+
head pointer input could be None as well for empty list
4+
Node is defined as
5+
6+
class Node(object):
7+
8+
def __init__(self, data=None, next_node=None):
9+
self.data = data
10+
self.next = next_node
11+
12+
return back the head of the linked list in the below method
13+
"""
14+
15+
def Insert(head, data):
16+
if(head == None):
17+
head = Node()
18+
head.data = data
19+
return head
20+
temp = Node()
21+
prev = Node()
22+
temp = head
23+
while(temp != None):
24+
prev = temp
25+
temp = temp.next
26+
t = Node()
27+
t.data = data
28+
prev.next = t
29+
return head
30+
31+
32+
33+
34+
35+
36+

0 commit comments

Comments
 (0)