merge two linked list in python

Solutions on MaxInterview for merge two linked list in python by the best coders in the world

showing results for - "merge two linked list in python"
Irene
30 Jun 2017
1class Node:
2    def __init__(self, x, nextNode = None):
3        self.val = x
4        self.next = nextNode
5 
6def printList(l):
7    value = []
8    while(l):
9        value.append(l.val)
10        l = l.next
11    print(' -> '.join(map(str, value)))
12 
13def addTwoNumbers(l1, l2):
14    """
15    :type l1: Node
16    :type l2: Node
17    :rtype: Node
18    """
19    sum = l1.val + l2.val
20    carry = int(sum / 10)
21 
22    l3 = Node(sum%10)
23    p1 = l1.next
24    p2 = l2.next
25    p3 = l3
26    while(p1 != None or p2 != None):
27        sum = carry + ( p1.val if p1 else 0) + ( p2.val if p2 else 0)
28        carry = int(sum/10)
29        p3.next = Node(sum % 10)
30        p3 = p3.next
31        p1 = p1.next if p1 else None
32        p2 = p2.next if p2 else None
33 
34    if(carry > 0):
35        p3.next = Node(carry)
36 
37    return l3
38 
39#789
40l1 = Node(9, Node(8, Node(7)))
41printList(l1)
42#478
43l2 = Node(8, Node(7, Node(4)))
44printList(l2)
45l3 = addTwoNumbers(l1, l2)
46printList(l3)   
47print()   
48#342
49l1 = Node(2, Node(4, Node(3)))
50printList(l1)
51#465
52l2 = Node(5, Node(6, Node(4)))
53printList(l2)
54l3 = addTwoNumbers(l1, l2)
55printList(l3)
Mila
14 May 2017
1class SinglyLinkedListNode:
2    def __init__(self, node_data):
3        self.data = node_data
4        self.next = None
5
6
7class SinglyLinkedList:
8    def __init__(self):
9        self.head = None
10        self.tail = None
11
12    def insert_node(self, node_data):
13        node = SinglyLinkedListNode(node_data)
14
15        if not self.head:
16            self.head = node
17        else:
18            self.tail.next = node
19
20        self.tail = node
21
22
23def print_singly_linked_list(node, sep):
24    while node:
25        print(str(node.data), end=' ')
26
27        node = node.next
28
29
30def printt(headd):
31    itr = headd
32    llstr = []
33    while itr:
34        llstr.append(itr.data)
35        itr = itr.next
36    return llstr
37
38
39def mergeLists(llist1, llist2):
40    ll1 = printt(llist1)
41    ll2 = printt(llist2)
42    ll3 = (ll1 + ll2)
43    ll3.sort()
44    lll = SinglyLinkedList()
45    for ii in ll3:
46        lll.insert_node(ii)
47    return lll.head
48
49
50if __name__ == '__main__':
51
52    llist1_count = int(
53        input("Enter the number of element to be in linked list 1: "))
54
55    llist1 = SinglyLinkedList()
56
57    print("Enter the elements to be added in list1 line by line")
58    for _ in range(llist1_count):
59        llist1_item = int(input())
60        llist1.insert_node(llist1_item)
61
62    print("\n")
63    llist2_count = int(
64        input("Enter the number of element to be in linked list 2: "))
65
66    llist2 = SinglyLinkedList()
67
68    print("Enter the elements to be added in list2 line by line")
69    for _ in range(llist2_count):
70        llist2_item = int(input())
71        llist2.insert_node(llist2_item)
72
73    llist3 = mergeLists(llist1.head, llist2.head)
74
75    print("\n")
76    print("The merged linked list value: ")
77    print_singly_linked_list(llist3, ' ')
78
79'''
80Output window:
81Enter the number of element to be in linked list 1: 3
82Enter the elements to be added in list1 line by line
831
842
853
86Enter the number of element to be in linked list 2: 2
87Enter the elements to be added in list2 line by line
883
894
90The merged linked list value:
911 2 3 3 4
92'''
queries leading to this page
addition of two numbers in linked listmerge 2 linked lists in python merge linked listssum of reverse order linked listhow to merge a linked list pythonformally 2c write a function that takes two linked lists as input and returns a linked list that represents the sum of both linked lists 2cmerge in between linked list python 27add two numbers represented by linked lists in caddition of two long integers using linked list in chow to merge two sorted linked lists in pythonsum of two link list integermerge two sorted linkedlist pythonadd two linked listadd two numbers linked listadd 2 linked listshow to initialize an empty singly linked list to store the values of adding two linked lists in pythonadding digits in linked list in c 2b 2bpython code of merge two sorted linkedlistaddadd two numbers as linkedlistsum of linked listsmerge two linked lists in order pythonyou are given two numbers represented by two different linked list you have to write a function to return the sum of both the numbers in a linked list add two numbers represented by linked lists what is 2a 2a represented in linked listadd two linked lists pythonhow to add two linked lists in c 2b 2bsum of likedlist with carygiven two numbers represented by two linked lists of size n and m the task is to return a sum list the sum list is a linked list representation of the addition of two input numbers adding big numbers using linked listadd two numbers as listadd two linked lists c 2b 2bpython add two numbers linked listadd numbers in linked listlarge number sum using linked listhe asked to write a code for adding two numbers represented by linked list 3fadd 2 numbers linked listsum of two linked list using stack cppfind the sum of two linked lists using stacktwo node value add c 2b 2bmerge 2 linked listsadd two numbers represented by linked listslinked list java add numbershow to add 2 single linked lists in pythonaddition using linked listadd two linkedlistadd two numbers as linked listadd two digit numbers linked listmerge two linked list in pythonadd numbers in linked list in ctwo linked list find the sum in reverse orderadd two number in ll gfgsum of two linked listadd two numbers 2b linked listwrite a python program to merge two linked listsum of a linked list in javascriptsum of two number python linked listadd two numbers represented by linked lists java2noviceadding digits in linked listmerge two sorted linked lists pythonadd two linked list of structureadding two linked lists and retiurn head of linked listaddtion of two linked list in javalinked list add two numbersadd two numbers represented by linked lists in javaadd number by user double linked list c 2b 2bpython merge 2 sorted linked listsadd two linked lists in pythonadd two linked listsadd two numbers linked list pythonadd two numbers represented by linked lists in c 2b 2bmerge in between linked list pythonlinked list sum merge linked lists in pythonadd two numbers represented by linked lists recursionadding a number an empty linked listhow to take input two linked list in c 2b 2badd 2 numbers concatenate two linked lists pythonadd two numbers as linkedlistmerging two linked list in pythonhow to add elements of 2 linked list in cgiven 2 linked lists constructed another linked list containing the sum of those 2 linked listsconcatenation of two linked lists pythonadd two linked list javahow to add two linked lists in pythonpython merge two linked listsadd two singly linked listaddition of long integers in c using linked listmerge 2 sorted linked list pythongeeksfor geeks add two digit numbersadd two numbers in a linked list in cadding 2 nos linked listhow to add two linked list items in pythonadd two numbers in a linked list without reversingaddition of long positive integers using circular linked listgettings the sum of 2 linked lists pythonadd two numbers represented by linked lists gfgmerge two linked list by pythonpython how to merge linked listssum reverse linked listhow to add two liked list node in pythonaddition of two numbers representing by linked listadd 2 numbers represented by linked listadd two numbers in linked list geeksforgeeksadd 2 linked lists pythontwo number sums in listmerging two linked lists in pythonadd 2 numbers as linked listgiven two numbers represented by two linked lists 2c write a function that returns the sum list how to add numbers on a linked list in pythonsum lists linked listsum of numbers on linked listadding 2 linked lists javahow to take input of two different linked lists in c 2b 2byou are given two linked lists representing two non negative integers the digits are stored in reverse order and each of their nodes contain a single digit add the two numbers and return it as a linked list merge linked list pythonadding two linkedlists in pythonadd two numbers on listmerge two linked lists in pythonaddition of numbers represented by linked listsadd two numbers in a linked list cpppython add two numbers stored in linked listsadd two numbers in a linked listhow to add two numbers to a listadd two numbers as linke listadding dogits in linked listpython merge two linkedlistadd two linked lists together javaadd two numbers as listsadd two binary numbers represented by linked lists sum linkedlistadd linked listadding twoo linked list pythonhow to sum a number into each number in a linked list pythongiven a list of values in the form of a linked list remove the elements from the list which sum of two numberlinkedlist add 2 numbers javahow to add two linked listsmerging linked lists pythonhow to add two linked lists in javaadd two numbers represented by linked listmerge two linked list pythonadd a double digit number to another number represented as linked listadding two tnumbers with link list javaadd two numbers in linked listhow to add two linked listhow to ad two number to listsum of two linked list using stacklinked list representat the given set of sorted numbers by a singly linked listaddition of long integers in c using linked list without header nodeadd two linkedlists into a set javapython adding two linked listsad d number by user double linked list c 2b 2bmerge two linked lists python6 add two numbers as linkedlistadd two linked lists representing numbersadd two link list leat codeadding two linked list in pythonadding two linked list given in reverse ordermerge two linked list with pythonadding number to linked lists javamerge two sorted linked lists in pythonsum link listadd numbers of a linked listsum of 2 linked listsreturns a linked list that represents the sum of both linked lists 2clinked list node with two valuessum of numbers in linked listmerge two linked listsaddition of two linked list in pythonhow to merge two linked list in pythonadding two numbers represented by linked listaddition of two linked listspython you are given two non empty linked lists representing two non negative integers the digits are stored in reverse order 2c and each of their nodes contains a single digit add the two numbers and return the sum as a linked list add two numbers aslistmerge two singly linked lists together pythonadd two digit numbers using linked listadd two numbers represented by linked list given two numbers represented by two linked lists 2c write a function that returns sum list the sum list is linked list representation of addition of two input numbers example 1 3ahow to add 2 linked lists in pythonhow to add 2 single link lists in pythonmerge two linked list in python