Answers for "merge two linked list in python"

0

add two linked lists python

class Node:
    def __init__(self, x, nextNode = None):
        self.val = x
        self.next = nextNode
 
def printList(l):
    value = []
    while(l):
        value.append(l.val)
        l = l.next
    print(' -> '.join(map(str, value)))
 
def addTwoNumbers(l1, l2):
    """
    :type l1: Node
    :type l2: Node
    :rtype: Node
    """
    sum = l1.val + l2.val
    carry = int(sum / 10)
 
    l3 = Node(sum%10)
    p1 = l1.next
    p2 = l2.next
    p3 = l3
    while(p1 != None or p2 != None):
        sum = carry + ( p1.val if p1 else 0) + ( p2.val if p2 else 0)
        carry = int(sum/10)
        p3.next = Node(sum % 10)
        p3 = p3.next
        p1 = p1.next if p1 else None
        p2 = p2.next if p2 else None
 
    if(carry > 0):
        p3.next = Node(carry)
 
    return l3
 
#789
l1 = Node(9, Node(8, Node(7)))
printList(l1)
#478
l2 = Node(8, Node(7, Node(4)))
printList(l2)
l3 = addTwoNumbers(l1, l2)
printList(l3)   
print()   
#342
l1 = Node(2, Node(4, Node(3)))
printList(l1)
#465
l2 = Node(5, Node(6, Node(4)))
printList(l2)
l3 = addTwoNumbers(l1, l2)
printList(l3)
Posted by: Guest on July-08-2020
2

merge two linked list in python

class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None


class SinglyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert_node(self, node_data):
        node = SinglyLinkedListNode(node_data)

        if not self.head:
            self.head = node
        else:
            self.tail.next = node

        self.tail = node


def print_singly_linked_list(node, sep):
    while node:
        print(str(node.data), end=' ')

        node = node.next


def printt(headd):
    itr = headd
    llstr = []
    while itr:
        llstr.append(itr.data)
        itr = itr.next
    return llstr


def mergeLists(llist1, llist2):
    ll1 = printt(llist1)
    ll2 = printt(llist2)
    ll3 = (ll1 + ll2)
    ll3.sort()
    lll = SinglyLinkedList()
    for ii in ll3:
        lll.insert_node(ii)
    return lll.head


if __name__ == '__main__':

    llist1_count = int(
        input("Enter the number of element to be in linked list 1: "))

    llist1 = SinglyLinkedList()

    print("Enter the elements to be added in list1 line by line")
    for _ in range(llist1_count):
        llist1_item = int(input())
        llist1.insert_node(llist1_item)

    print("\n")
    llist2_count = int(
        input("Enter the number of element to be in linked list 2: "))

    llist2 = SinglyLinkedList()

    print("Enter the elements to be added in list2 line by line")
    for _ in range(llist2_count):
        llist2_item = int(input())
        llist2.insert_node(llist2_item)

    llist3 = mergeLists(llist1.head, llist2.head)

    print("\n")
    print("The merged linked list value: ")
    print_singly_linked_list(llist3, ' ')

'''
Output window:
Enter the number of element to be in linked list 1: 3
Enter the elements to be added in list1 line by line
1
2
3
Enter the number of element to be in linked list 2: 2
Enter the elements to be added in list2 line by line
3
4
The merged linked list value:
1 2 3 3 4
'''
Posted by: Guest on August-03-2021

Code answers related to "merge two linked list in python"

Python Answers by Framework

Browse Popular Code Answers by Language