Python Program To Add Two Numbers Represented By Linked Lists- Set 1 Last Updated : 18 May, 2022 Comments Improve Suggest changes Like Article Like Report Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1->4->0->5 // represents number 1405 Explanation: 563 + 842 = 1405 Input: List1: 7->5->9->4->6 // represents number 75946List2: 8->4 // represents number 84Output: Resultant list: 7->6->0->3->0// represents number 76030Explanation: 75946+84=76030 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: Traverse both lists and One by one pick nodes of both lists and add the values. If the sum is more than 10 then make carry as 1 and reduce sum. If one list has more elements than the other then consider the remaining values of this list as 0. The steps are: Traverse the two linked lists from start to endAdd the two digits each from respective linked lists.If one of the lists has reached the end then take 0 as its digit.Continue it until both the end of the lists.If the sum of two digits is greater than 9 then set carry as 1 and the current digit as sum % 10 Below is the implementation of this approach. Python # Python program to add two numbers # represented by linked list # Node class class Node: # Constructor to initialize the # node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to insert a new node at # the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Add contents of two linked lists and # return the head node of resultant list def addTwoLists(self, first, second): prev = None temp = None carry = 0 # While both list exists while(first is not None or second is not None): # Calculate the value of next digit # in resultant list # The next digit is sum of following # things # (i) Carry # (ii) Next digit of first list (if # there is a next digit) # (iii) Next digit of second list (if # there is a next digit) fdata = 0 if first is None else first.data sdata = 0 if second is None else second.data Sum = carry + fdata + sdata # Update carry for next calculation carry = 1 if Sum >= 10 else 0 # Update sum if it is greater than 10 Sum = Sum if Sum < 10 else Sum % 10 # Create a new node with sum as data temp = Node(Sum) # if this is the first node then set # it as head of resultant list if self.head is None: self.head = temp else: prev.next = temp # Set prev for next insertion prev = temp # Move first and second pointers to # next nodes if first is not None: first = first.next if second is not None: second = second.next if carry > 0: temp.next = Node(carry) # Utility function to print the # linked LinkedList def printList(self): temp = self.head while(temp): print temp.data, temp = temp.next # Driver code first = LinkedList() second = LinkedList() # Create first list first.push(6) first.push(4) first.push(9) first.push(5) first.push(7) print "First List is ", first.printList() # Create second list second.push(4) second.push(8) print " Second List is ", second.printList() # Add the two lists and see result res = LinkedList() res.addTwoLists(first.head, second.head) print " Resultant list is ", res.printList() # This code is contributed by Nikhil Kumar Singh(nickzuck_007) Output:First List is 7 5 9 4 6 Second List is 8 4 Resultant list is 5 0 0 5 6 Complexity Analysis: Time Complexity: O(m + n), where m and n are numbers of nodes in first and second lists respectively. The lists need to be traversed only once.Space Complexity: O(m + n). A temporary linked list is needed to store the output number Related Article: Add two numbers represented by linked lists | Set 2 Please refer complete article on Add two numbers represented by linked lists | Set 1 for more details! Comment More infoAdvertise with us Next Article Python Program To Add Two Numbers Represented By Linked Lists- Set 1 kartik Follow Improve Article Tags : Linked List Python Programs DSA Microsoft Amazon Flipkart Qualcomm Snapdeal Accolite MakeMyTrip Python-DSA +7 More Practice Tags : AccoliteAmazonFlipkartMakeMyTripMicrosoftQualcommSnapdealLinked List +4 More Similar Reads Python Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no 5 min read Python Program To Multiply Two Numbers Represented By Linked Lists Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists. Examples: Input: 9->4->6 8->4Output: 79464Input: 3->2->1 1->2Output: 3852Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Pyt 3 min read Python Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Recommended: Please solve it on "PRACTICE" first, before moving on to 4 min read Python program to add two binary numbers Given two binary numbers, write a Python program to compute their sum. Examples: Input: a = "11", b = "1" Output: "100" Input: a = "1101", b = "100" Output: 10001Approach: Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum become 4 min read Python Program To Merge Two Sorted Lists (In-Place) Given two sorted lists, merge them so as to produce a combined sorted list (without using extra space).Examples: Input: head1: 5->7->9 head2: 4->6->8 Output: 4->5->6->7->8->9 Explanation: The output list is in sorted order. Input: head1: 1->3->5->7 head2: 2->4 Output: 1->2->3->4->5->7 Explanation: T 5 min read Python Program for Find a triplet from three linked lists with sum equal to a given number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple " 4 min read Python Program For Merge Sort For Doubly Linked List Given a doubly linked list, write a function to sort the doubly linked list in increasing order using merge sort.For example, the following doubly linked list should be changed to 24810 Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Merge sort for singly linked l 3 min read Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes 4 min read Python Program For Cloning A Linked List With Next And Random Pointer- Set 2 We have already discussed 2 different ways to clone a linked list. In this post, one more simple method to clone a linked list is discussed. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. The idea is to use Hashing. Below is algorithm. Traverse the original link 3 min read Like