Giter Club home page Giter Club logo

linked-list-number-adder's Introduction

Linked List Number Adder

This C++ project implements an algorithm to add two numbers represented as reverse-ordered linked lists. Each node of the linked list contains a single digit, and the digits are stored in reverse order.

Problem Statement

Given two non-empty linked lists representing two non-negative integers, where the digits are stored in reverse order, the task is to add these two numbers and return the sum as a linked list.

Algorithm

The addTwoNumbers(ListNode* l1, ListNode* l2) function is the core of this project, where the numbers represented by linked lists l1 and l2 are added.

Logic

  1. Initialization: A dummy head node is used to simplify list operations. A carry variable is initialized to 0.
  2. Traversal: Iterate through both lists simultaneously while handling any carry from the previous addition.
  3. Addition: For each pair of nodes, sum the digits along with the carry. Update the carry for the next iteration.
  4. Node Creation: Create a new node with the digit part of the sum and add it to the resulting list.
  5. Carry Handling: After traversing both lists, if there is a remaining carry, it is added as a new node to the result list.

Time Complexity

  • O(max(n, m)): Here, n and m represent the lengths of the two input lists. The complexity is linear to the length of the longer list.

Space Complexity

  • O(max(n, m)): The length of the new list is at most max(n, m) + 1 (due to a potential carry).

Potential Improvements

  • Optimized Memory Management: Implement advanced memory handling techniques, especially for large lists.
  • Alternative Implementations: Exploring different methods, such as a recursive approach, could provide educational insights, though they might not improve efficiency.

Utility Functions

  • createList(const vector<int>& nums): Generates a linked list from a vector of integers.
  • printList(ListNode* head): Outputs the linked list to the console.
  • freeList(ListNode* head): Frees dynamically allocated memory of the list.

Test Cases

Included in the main function are several test cases demonstrating the algorithm:

  1. Numbers with equal lengths.
  2. Numbers with unequal lengths.
  3. Cases involving zero.
  4. Large numbers.

Usage

Compile and run the C++ file to observe the algorithm in action with the provided test cases.

Contributions

Contributions are welcome. Feel free to fork the repository, suggest improvements, or add additional test cases.

linked-list-number-adder's People

Contributors

danieldotwav avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.