hey starting data structers through pointers , i don't know how to reverse a singly linklist?
pls help

Recommended Answers

All 18 Replies

ist u try on ur own where ever u will stuck i will help u .it iz quite easy

commented: don't use sms-speak +0

thanks for ur reply
but i have no idea about reverse a linklist,b'coz when u swap the last element with first one ,, don't know how to switch to second last element :(
please help

Have you learned recursion? I think you could reverse an empty list quite easily wouldn't you? You just figure out how to reverse the list consisting on first node and rest of list reversed.

Non-recursive reversing requires just that you remember the previous node and consider carefully corner cases like empty list.

commented: Recursion is certainly the way to go +8

do you know how to create linked list ? if yes then to reverse you can use this post Click Here you just take your list and while you traverse it just move to new in reversed order

One basic algorithm is basically just reversing the next-pointer between two adjacent nodes while traversing the list, and at the end, you swap the head and tail pointers. To do this, you need to keep three node pointers, for three consecutive nodes (the last may be NULL). If you have node1, node2, and node3, so that node1->next == node2 and node2->next == node3 in the original list, then you can reverse the order of the first two nodes with node2->next = node1;, and then proceed forward with node1 = node2; node2 = node3; and node3 = node2->next;, unless node3 was NULL, then you just finish.

I can't give more details if you don't show some efforts to do this on your own.

lets take a temporary variable temp
swap it with the top untill temp->next =null
simple

successfully make the program to reverse link list.
but what if question arise to reverse the link list without using pointers?

but what if question arise to reverse the link list without using pointers?

Is it still a singly linked list? Are you allowed to create another variable or data structure to hold the reversed data? Is this a hypothetical question? ;)

but what if question arise to reverse the link list without using pointers?

Well, a linked-list is just a chain of nodes pointing to each other. You can't really do anything with a linked-list without using pointers. To start, you have to use the "head" pointer, so that's already "using pointers". Or did I misunderstand your question? What do you mean by "without using pointers"?

I think he meant without pointer surgery, such as if you traverse and swap the data.

thanks for ur reply
but even i don't know what is the meaning to reverse the linklist without using pointer
i can understand that it is not possible to traverse linklist without using pointers.
i read that question and not able to answer it,that's y put my article here.
pls help

read that question and not able to answer it,that's y put my article here.

If it's a question out of a book, post that question in its entirety exactly as written. Paraphrasing is a good way to miss important details that will cause us to give you unproductive help.

How do you reverse a linked list without using any C pointers?

as u know that linked list has two types singly and double in singly there are two parts one contains data and other one address of another node so it is impossible to traverse the linked list without using the pointers and in double linked list it has three parts one contains address of previous node and rest two contains same as of singley linked list

How do you reverse a linked list without using any C pointers?

Reverse data structure that is linked by pointers without using pointers ? Really ? :)) No cant.

here is a code for reversing link list recursively.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct node{
        int x;
        struct node *next;
        } node;
        node *head1;
node *reverser(node *head) // this is recursive function
{
     node *curr,*temp;
     curr=head;
     if(curr->next==NULL){
     head1=curr;
     return curr;}
     head=reverser(curr->next);
     head->next=curr;
     curr->next=NULL;
     return curr;
}

int main()
{
     int n,c=0;
    node *head,*curr,*ne;
    printf("enter the number of numbers to b used");
    scanf("%d",&n);
    while(c<n){
    if(c==0){
    head=(node*)malloc(sizeof(node));
     printf("enter the head number");
     scanf("%d",&head->x);
    curr=head;
    c++;}

  else{
       ne=(node*)malloc(sizeof(node));
       printf("enter the current number");
       scanf("%d",&ne->x);
       curr->next=ne;
       curr=ne;
       c++;
       }      }
       curr->next=NULL;
       reverser(head);
       printf("list in reverse order is\n");
       while(head1->next!=NULL){
       printf("%d\n",head1->x);
       head1=head1->next;      }
       printf("%d",head1->x);
       getch();
  }


if u dont understand anything feel free to ask.

How do you reverse a linked list without using any C pointers?

One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the "next" pointer fields to traverse through the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).
Source

Try implementing this code

        void reverse(node **ptr)
        {
            node *current,*prev,*temp;
            current=*ptr;
            prev=temp=NULL;
            while(current!=NULL)
                {
                    temp=prev;
                    prev=current;
                    current=current->next;
                    prev->next=temp;
                }
            *ptr=prev;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.