class
Node:
def
__init__(
self
, data):
self
.data
=
data
self
.
next
=
None
def
insertNode(head, tail, d):
if
head
is
None
:
newNode
=
Node(d)
head
=
newNode
tail
=
newNode
newNode.
next
=
newNode
else
:
temp
=
Node(d)
temp.
next
=
tail.
next
tail.
next
=
temp
tail
=
tail.
next
return
head, tail
def
printList(head):
curr
=
head
if
head
is
None
:
print
(
"List is Empty"
)
return
while
curr.
next
!
=
head:
print
(curr.data, end
=
' '
)
curr
=
curr.
next
print
(curr.data)
def
concat(head1, head2):
temp
=
head1.
next
head1.
next
=
head2.
next
head2.
next
=
temp
if
__name__
=
=
'__main__'
:
head1
=
None
tail1
=
None
head1, tail1
=
insertNode(head1, tail1,
1
)
head1, tail1
=
insertNode(head1, tail1,
2
)
head1, tail1
=
insertNode(head1, tail1,
3
)
head1, tail1
=
insertNode(head1, tail1,
4
)
head1, tail1
=
insertNode(head1, tail1,
5
)
head2
=
None
tail2
=
None
head2, tail2
=
insertNode(head2, tail2,
6
)
head2, tail2
=
insertNode(head2, tail2,
7
)
head2, tail2
=
insertNode(head2, tail2,
8
)
head2, tail2
=
insertNode(head2, tail2,
9
)
head2, tail2
=
insertNode(head2, tail2,
10
)
print
(
"Before Concatenation, First circular linked list: "
)
printList(head1)
print
(
"Before Concatenation, Second circular linked list: "
)
printList(head2)
concat(head1, head2)
print
(
"After Concatenation, First circular linked list: "
)
printList(head1)
print
(
"After Concatenation, Second circular linked list: "
)
printList(head2)