Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Reverse the link list without creating new list?
Posted
Updated 2-Sep-10 21:12pm
v2
Comments
ThatsAlok 3-Sep-10 5:47am    
Reason for my vote of 4
Given me chance to relive my ol's college days
Sandeep Mewara 3-Sep-10 11:25am    
Reason for my vote of 3
No Effort! Google?

today is your lucky day! :-), though it is in c++!

C#
#include "stdafx.h"
#include <iostream>
using namespace std;

struct Node
{
    int Data;
    Node* Next;

    Node()
    {
        Data =-1;
        Next= NULL;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    // fill data into link List
    Node* First= NULL, *Next=NULL,*Temp;

    for(int i =10;i<20;i++)
    {
        Temp = new Node();
        Temp->Data = i;

        Next = First;

        if(Next==NULL)
        {
            First = Temp;
        }
        else
        {
            while((Next->Next!=NULL))
            {
                Next=Next->Next;
            }
            Next->Next = Temp;
        }
    }


    //display data
    cout<<"Display Data"<<endl;
    Next = First;
    while((Next!=NULL))
    {
        cout<<Next->Data<<endl;
        Next=Next->Next;
    }
    // Reverse it  now


    Node* Last=NULL,*ltTemp;



    while(First->Next!=NULL)
    {

        ltTemp = First;
        Temp = First->Next;

        while((Temp->Next!=NULL))
        {
            if(Temp)
                Temp=Temp->Next;
            if(ltTemp)
                ltTemp= ltTemp->Next;
        }
        // Plug the last one
        ltTemp->Next= NULL;

        // Place in reverse order

        if(Last== NULL)
        {
            Last=Temp;

        }
        else
        {
            Next = Last ;
            while((Next->Next!=NULL))
            {
                Next=Next->Next;
            }
            Next->Next = Temp;
        }
    }

    if(Temp)
        Temp->Next= First;

    //display data
    cout<<"Display Data reverse order"<<endl;
    Next = Last;
    while((Next!=NULL))
    {
        cout<<Next->Data<<endl;
        Next=Next->Next;
    }

    /// delete all the allocated memory
    Next = First;
    if(Next)
    while((Next!=NULL))
    {
        Temp = Next;
        Next=Next->Next;
        delete Temp;
    }

    if(Next)
        delete Next;

}
 
Share this answer
 
one more :-).. this time this logic work faster

C#
#include "stdafx.h"
#include <iostream>
using namespace std;

struct Node
{
    int Data;
    Node* Next;

    Node()
    {
        Data =-1;
        Next= NULL;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    // fill data into link List
    Node* First= NULL, *Next=NULL,*Temp;

    for(int i =10;i<20;i++)
    {
        Temp = new Node();
        Temp->Data = i;

        Next = First;

        if(Next==NULL)
        {
            First = Temp;
        }
        else
        {
            while((Next->Next!=NULL))
            {
                Next=Next->Next;
            }
            Next->Next = Temp;
        }
    }


    //display data
    cout<<"Display Data"<<endl;
    Next = First;
    while((Next!=NULL))
    {
        cout<<Next->Data<<endl;
        Next=Next->Next;
    }
    // Reverse it  now


    Node* Last=NULL,*ltTemp;

    Temp= First;
    while(Temp)
    {
        ltTemp = Temp->Next;

        if(Last)
        {
            Temp->Next=Last;
        }
        else
            Temp->Next=NULL;

        Last = Temp;
        Temp=ltTemp;

    }

    //display data
    cout<<"Display Data reverse order"<<endl;
    Next = Last;
    while((Next!=NULL))
    {
        cout<<Next->Data<<endl;
        Next=Next->Next;
    }

    /// delete all the allocated memory
    Next = First;
    if(Next)
    while((Next!=NULL))
    {
        Temp = Next;
        Next=Next->Next;
        delete Temp;
    }

    if(Next)
        delete Next;

}
 
Share this answer
 
is it homework??.. have you tried logic urself?

little code snippet:-

say your linklist is created with this class :-
struct Node
{
int Data;
struct Node * Next;
};

let me give a clue, use temp Node variable to reverse.
 
Share this answer
 
Comments
Sandeep Mewara 3-Sep-10 11:27am    
You shouldn't have provided the codebase until seeing his effort. Clearly it's a homework. Even if it's not, he needs to put some effort to try first. Further, doing a simple Google search can provide hunderds of links!
Please don't encourage such things from next time.
ThatsAlok 3-Sep-10 11:55am    
@sandeep, nothing personel.. i have done that for me...i have nothing to do for quite some days... so i just take a work and done it:-)
Do you mean: "how to reverse a linked list without creating a new list?".
Google is you friend, see, for instance: Reverse a singly linked list (Stack Overflow).
:)
 
Share this answer
 
Once the end of the list is reached, traverse backwards, till you reach the starting of the list. You need to know the starting and ending of your list.

Regards,
LG.
 
Share this answer
 
Comments
Peter_in_2780 3-Sep-10 3:38am    
Reason for my vote of 1
If it's a singly linked list, how the @#$% are you going to traverse it backwards?
ThatsAlok 3-Sep-10 5:34am    
@peter might be new type of LinkedList :-) i.e. with Index he heh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900