Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78f3fc4

Browse files
Concatenate single linked lists in Python
1 parent 9c73a09 commit 78f3fc4

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

‎linked-list/concatenate-single-lists.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright (C) Deepali Srivastava - All Rights Reserved
2+
# This code is part of DSA course available on CourseGalaxy.com
3+
4+
class Node:
5+
6+
def __init__(self,value):
7+
self.info = value
8+
self.link = None
9+
10+
11+
class SingleLinkedList:
12+
13+
def __init__(self):
14+
self.start = None
15+
16+
def display_list(self):
17+
if self.start is None:
18+
print("List is empty")
19+
return
20+
else:
21+
print("List is : ")
22+
p = self.start
23+
while p is not None:
24+
print(p.info , " ", end='')
25+
p = p.link
26+
print()
27+
28+
def insert_at_end(self, data):
29+
temp = Node(data)
30+
if self.start is None:
31+
self.start = temp
32+
return
33+
34+
p = self.start
35+
while p.link is not None:
36+
p = p.link
37+
p.link = temp
38+
39+
40+
def create_list(self):
41+
n = int(input("Enter the number of nodes : "))
42+
if n == 0:
43+
return
44+
for i in range(n):
45+
data = int(input("Enter the element to be inserted : "))
46+
self.insert_at_end(data)
47+
48+
def concatenate(self, list2):
49+
if self.start is None:
50+
self.start = list2.start
51+
return
52+
53+
if list2.start is None:
54+
return
55+
56+
p = self.start
57+
while p.link is not None:
58+
p = p.link
59+
60+
p.link = list2.start
61+
62+
#########################################################################################
63+
64+
list1 = SingleLinkedList()
65+
list2 = SingleLinkedList()
66+
67+
print("Enter first list :- ")
68+
list1.create_list()
69+
print("Enter second list :- ")
70+
list2.create_list()
71+
72+
print("First "); list1.display_list()
73+
print("Second "); list2.display_list()
74+
75+
list1.concatenate(list2)
76+
print("First "); list1.display_list()
77+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /