Add Two Numbers – II

Let’s discuss a very interesting problem add two numbers represented by linked lists.

Problem Statement

Given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and least significant digit comes last. Each of the linked list node contain a single digit.

We have to write a code to add the two numbers and return it as a linked list.

Both the numbers do not contain any leading zero, except the number 0 itself.

NOTE: Reversing a linked list is not allowed.

For Example –

List 1 : 7 -> 2 -> 4 -> 3 -> NULL

List 2 : 5 -> 6 -> 4 -> NULL

Result : 7 -> 8 -> 0 -> 7 -> NULL

Explanation : The first linked list is representing the number 7243 and the second linked is representing the number 564. If we add these two numbers we get 7807, which we have to return in the form of a linked list.

Sort a Stack using Recursion

In this problem, We have to write a code to sort a stack using recursion. We have to sort in a descending order (Top of the stack has the greatest element).

We don’t have to use any loop constructs ( for, while etc) or additional data structure.

For Example –

In this example, You can see after sorting the stack, the element which has greater value is at the top of the stack.

Sort a stack using recursion

Reverse a Stack using Recursion

How to reverse a stack using Recursion? In this tutorial, I am going to discuss this interesting problem.

Given a stack of integers, we have to write a code to reverse them using recursion. For solving this problem, we don’t have to use any loop constructs (For, While etc.) or any additional data structure.

You can use stack methods like push, pop, isEmpty to solve this problem.

Reverse a stack using recursion