Reverse Order Number Addition in Python

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This Python challenge requires adding two numbers whose digits are stored in a linked list in reverse order.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/linked_lists("`Linked Lists`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} algorithm/linked_lists -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/conditional_statements -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/while_loops -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/lists -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/tuples -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/function_definition -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/default_arguments -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/classes_objects -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/constructor -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/polymorphism -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/encapsulation -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/iterators -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} python/build_in_functions -.-> lab-268840{{"`Reverse Order Number Addition in Python`"}} end

Add Reverse

Problem

Given two non-circular, singly linked lists, add the values of each node in reverse order and return the result in a linked list in reverse order too. If one of the inputs is None, return None for an invalid operation. The numbers can fit in memory and we can assume we already have a linked list class that can be used for this problem.

Requirements

To complete this challenge, the following requirements must be met:

  • The linked list is non-circular and singly linked.
  • The return value must be in reverse order.
  • If one of the inputs is None, return None for an invalid operation.
  • The numbers can fit in memory.
  • We can assume we already have a linked list class that can be used for this problem.
  • The linked list can fit in memory.

Example Usage

The following examples demonstrate the usage of this challenge:

  • Empty list(s) -> None
  • Add values of different lengths
    • Input 1: 6->5->None
    • Input 2: 9->8->7
    • Result: 5->4->8
  • Add values of same lengths
    • Exercised from values of different lengths
    • Done here for completeness

Summary

The linked list is non-circular and singly linked. The return value must be in reverse order. If one of the inputs is None, return None for an invalid operation. The numbers can fit in memory and we can assume we already have a linked list class that can be used for this problem.

Other Algorithm Tutorials you may like