REVERSE LINK LIST

Concepts Used

Reversing a linked list,pointers

Difficulty Level

Hard.

Problem Statement :

Reverse a linked list from position M to N. Do it in-place and in one-pass.

Example:

Given 1βˆ’>2βˆ’>3βˆ’>4βˆ’>5βˆ’>NULL,
M=1 and N=3,
Output:  3βˆ’>2βˆ’>1βˆ’>4βˆ’>5βˆ’>NULL.

See original problem statement here

EXPLANATION:

Since we have to reverse a part of the given linked list obviously we need the best sites to learn programming languages to understand the concept of reversing the linked list.. We need to find the mth node and pass it to the reverse function (which will reverse the given part). But, before passing the mth node we need to traverse to the nth node and cut its link with (n+1)th node if present.

Also we have to save the (m-1) node and also (n+1)th node address so that we can link the reversed part of the linked list again with the original linked list.

There will be four variables.
Prev β†’ for storing the previous node, i.e. (m-1)th node.

Start β†’ for storing the starting(mth) node of reversal.

End β†’ for storing the ending node(nth) of reversal.

Next β†’ for storing the next node, i.e. (n+1)th node.

Now, we will pass Start to the reverse function and then will attach the reversed part with Start and End to get the reversed linked list.

SOLUTIONS:

[TABS_R id=2253]
[forminator_quiz id=”2254″]

Previous post Play With Brackets
Next post MONOPOLY ONCE AGAIN

Leave a Reply

Your email address will not be published. Required fields are marked *