Reverse a Doubly Linked List

PROBLEM :

Given a doubly linked list, the task is to Reverse the list. It should be done in-place without altering the nodes' values.

In this function problem, the function reverse takes one argument: Address of the head of the linked list. The function should contain the logic to reverse the linked list.

The linked list node structure has 3 fields, a data part which stores the data, a next pointer which points to the next element of the linked list and a previous pointer which points to the previous node of the linked list.

There are multiple test cases. For each test case, this function will be called individually.

Note:  The Input/Ouput format and Example given below is used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console.

Input:
The first line contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of two lines. First line of each test case contains an integer N denoting the size of the linked list. Second line of each test case consists of 'N' space separated integers denoting the elements of the linked list.

Output:
Print the linked list in Reverse order.

Constraints:

1<=T<=200
1<=N<=200

Example:
Input:
2
3
2 4 5
4
1 4 6 8

Output:

5 4 2
8 6 4 1

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/* a node of the doubly linked list */
/*struct node
{
  int data;
  struct node *next;
  struct node *prev;  
};*/

void reverse(struct node **head_ref)
{
    struct node *one,*two,*three ;
   
    one=(*head_ref) ;
    two=one->next ;
    three=two->next ;
   
    one->next=NULL ;
    one->prev=two ;
   
    two->next=one ;
    two->prev=three ;
   
    while(three!=NULL)
    {
        one=two ;
        two=three ;
        three=three->next ;
       
        two->next=one ;
        two->prev=three ;
    }
   
    (*head_ref)=two ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )