
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find numbers represented as linked lists in Python
Suppose we have two singly linked list L1 and L2, each representing a number with least significant digits first, we have to find the summed linked list.
So, if the input is like L1 = [5,6,4] L2 = [2,4,8], then the output will be [7, 0, 3, 1, ]
To solve this, we will follow these steps:
carry := 0
res := a new node with value 0
curr := res
while L1 is not empty or L2 is not empty or carry is non-zero, do
l0_val := value of L1 if L1 is not empty otherwise 0
l1_val := value of L2 if L2 is not empty otherwise 0
sum_ := l0_val + l1_val
carry := quotient of (sum_ + carry) / 10
add_val := remainder of (sum_ + carry) / 10
curr.next := a new node with value add_val
curr := next of curr
L1 := next of L1 if L1 is not empty otherwise null
L2 := next of L2 if L2 is not empty otherwise null
next of curr := null
return next of res
Let us see the following implementation to get better understanding:
Example
class ListNode: def __init__(self, data, next = None): self.val = data self.next = next def make_list(elements): head = ListNode(elements[0]) for element in elements[1:]: ptr = head while ptr.next: ptr = ptr.next ptr.next = ListNode(element) return head def print_list(head): ptr = head print('[', end = "") while ptr: print(ptr.val, end = ", ") ptr = ptr.next print(']') class Solution: def solve(self, L1, L2): carry = 0 res = ListNode(0) curr = res while L1 or L2 or carry: l0_val = L1.val if L1 else 0 l1_val = L2.val if L2 else 0 sum_ = l0_val + l1_val carry, add_val = divmod(sum_ + carry, 10) curr.next = ListNode(add_val) curr = curr.next L1 = L1.next if L1 else None L2 = L2.next if L2 else None curr.next = None return res.next ob = Solution() L1 = make_list([5,6,4]) L2 = make_list([2,4,8]) print_list(ob.solve(L1, L2))
Input
[5,6,4], [2,4,8]
Output
[7, 0, 3, 1, ]
- Related Articles
- Add two numbers represented by linked lists?
- Multiply two numbers represented as linked lists into a third list in C++
- Multiply two numbers represented by Linked Lists in C++
- Program to add two numbers represented as strings in Python
- Program to add two polynomials given as linked lists using Python
- Program to find union of two given linked lists in Python
- Program to merge in between linked lists in Python
- Why are numbers represented as objects in python?
- Program to interleave list elements from two linked lists in Python
- Python Program to Check whether 2 Linked Lists are Same
- Add 1 to a number represented as linked list?
- Python Program to Find the first Common Element between the 2 given Linked Lists
- Python Program to Add Corresponding Positioned Elements of 2 Linked Lists
- Add 1 to a number represented as a linked list?
- Program to expand string represented as n(t) format in Python
