Skip to main content

Adding one to number represented as Linked List


In this post, we will discuss a method to add one to a number which is represented using nodes of the linked list. 
This problem becomes very simple if the given linked list is considered to be doubly linked list as it is easier to add one to the number at the end of the linked list and traverse the list from backward direction till carry becomes zero. but if the given linked list is a singly linked list then we have to first reverse the given linked list and then add one to the first node. So considering our linked list is a simple linked list we follow the below steps.

Algorithm:

  1. Reverse the linked list.
  2. Add one to the data field of the first node. 
  3. While carry is not zero keep adding carry to the next node's data field 
  4. if you reach the end of the linked list and the carry is not zero then we add a new node to the end of the linked list. 
  5. Reverse the final linked list and return the head node of the linked list.


Adding one to a number represented as Linked List image

C++ Program

Sample input and output to check the program



Comments

Popular posts from this blog

Home Page

List of All Programs The Following is the List of all the programs on my Blog Math Programs Square Root of a number using Babylonian Method Finding The Next Smallest Palindrome Finding the Armstrong Numbers Factorial of a number GCD using Euclid's Algorithm Check if a number is Fibonacci Number or not LCM of 2 numbers Trailing Zeros in factorial of a number Sorting Algorithms Bubble Sort Algorithm Selection Sort Algorithm Insertion Sort Algorithm Shell Sort Algorithm Counting Sort Algorithm Linked List Programs Simple Singly Linked List Linked List in C++ Linked List in Python Linked List in Java Doubly Linked List Finding Kth element from the end of Linked List Delete a node from Linked List Delete Kth element from the end of Linked List Rotate Linked List in an Anti-clockwise direction Reversing first K nodes of a Linked List Binary Search Tree Left View of Binary Tree Righ...

Hashing with Quadratic Probing

Hashing is a technique used for storing , searching and removing elements in almost constant time. Hashing is done with help of a hash function that generates index for a given input, then this index can be used to search the elements, store an element, or remove that element from that index. A hash function is a function that is used to map the data elements to their position in the data structure used. For example if we use an array to store the integer elements then the hash function will generate position for each element so that searching, storing and removing operation on the array can be done in constant time that is independent of the number of elements in the array. For better look at the example below. now we face a problem if for 2 numbers same position is generated example consider elements 1 and 14 1 % 13 = 1 14 % 13 = 1 so when we get 1 we store it at the first position, but when we get 14 we see that the position 1 is already taken, this is a case of colli...

Encrypt files using Vigenere Cipher

In this post we discuss a method to encrypt your files using  Vigenère Cipher . The  Vigenère cipher  is a method of  encrypting   alphabetic  text by using a series of interwoven  Caesar ciphers  based on the letters of a keyword.  It is a form of  polyalphabetic substitution . Even though the vigenere cipher is easy to understand and implement, for three centuries it resisted all attempts to break it. In order to encrypt a given message, a table of alphabets can be used, termed a  tabula recta , The following is the image of the Table. the above table   consists of the alphabet written out 26 times in different rows, each alphabet shifted cyclically to the left compared to the previous alphabet.  At different points in the encryption process, the cipher uses a different alphabet from one of the rows. The alphabet used at each point depends on a repeating keyword. Now let us consider an example...