Write a python function which accepts two linked lists containing integer data and an integer, n and merges two linked lists, su | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a python function which accepts two linked lists containing integer data and an integer, n and merges two linked lists, su

10th May 2021, 1:46 PM
Neel Tilak
Neel Tilak - avatar
3 Answers
+ 1
Post your code first
10th May 2021, 1:53 PM
Atul
Atul - avatar
+ 1
Neel Tilak Where's your attempt?
10th May 2021, 1:57 PM
sarada lakshmi
sarada lakshmi - avatar
0
import math class Node: def __init__(self, data): self.data = data self.next = None def newNode( key): temp = Node(key) temp.data = key temp.next = None return temp def printList( node): while (node != None): print(node.data, end = " ") node = node.next def merge( h1, h2): if (h1 == None): return h2 if (h2 == None): return h1 if (h1.data < h2.data): h1.next = merge(h1.next, h2) return h1 else: h2.next = merge(h1, h2.next) return h2 if __name__=='__main__': head1 = newNode(1) head1.next = newNode(3) head1.next.next = newNode(5) head2 = newNode(0) head2.next = newNode(2) head2.next.next = newNode(4) mergedhead = merge(head1, head2) printList(mergedhead)
11th May 2021, 6:15 AM
Neel Tilak
Neel Tilak - avatar