How do you reverse a singly-linked list in C?
This is what my linked lists look like: typedef struct { int data; struct NODE* next; } NODE
I'm just starting to read about C and C++ with the intention of studying objective C further after those 2, I'll answer you when I'm done studying them :) jkjk lol, idk how to go about this, it's not similar to any of the code syntax i'm familiar with, have you checked google or any C forums?
it's alright... I just need the basic idea :-D
What exactly do you mean with "reverse"?
turn this: [head]->...->[tail] into this: [head]<-...<-[tail] so that the tail node is the new head
wait, so like if u have like an array with a list of variables, you make the list read backward? say [0,1,2,3,4] turns to [4,3,2,1,0] ..... not sure what u mean by reverse tbh
Hm, have you tried anything of your own? (this is really elementary)
@sasogeek right, but with a singly linked list instead of an array :-D @ffm This is the very first time I've done anything with linked lists. In fact, that definition of a node up there was the first time I tried implementing a linked list.
so I don't know what the -> operator in C does
I don't even know how to traverse a singly-linked list.
or insert/delete etc.
lol
Then learn traversing, inserting and deleting first.
I could use some pointers (get it? pointers! hahahaha)
Google is your friend ;)
I apologize for the delay; I had to make some drawings.
Alright here is my linked list traversal code: void traverse_list(NODE* node) { for (;node->next; node = node->next); }
Well, when it comes to programming, there are many ways to implement the same thing. In the spur of the moment, I came up with one way to accomplish this. Assume you have the attached linked-list.
That's a singly linked list :-D
The first step of my algorithm would be to access the very first element of the list, assign its pointer to a "helper" one, and set the pointer of the first link to null.
Sorry, OS issues.
So these are your first steps: 1. Define a temporary pointer variable, and assign it to the 'next' field of the head (i.e. have it point to the node after the head) 2. Set the 'next' field of the head to NULL Now the head is lonely and the new head is the node pointed to by the temporary pointer variable.
Site is buggy :-D
My code looks like this now: \[# NODE* reverse_list(NODE* head) { NODE* temp; temp = head->next; head->next = NULL; } \]
The second step would be to set the pointer of the element the helper pointer points to to the head pointer (in this case, the first element), set the head pointer to the helper pointer, and move on to the next element with the helper pointer.
Recursion seem to be a good choice here.
The third step would be to repeat this process until your helper pointer reaches the null pointer, in which case you would have successfully reversed your linked-list.
@FFM I think that's one property of singly-linked lists I should know; that something about them makes recursive solutions suitable for doing stuff with them or something,
An implementation of this algorithm would look something like this: void reverse(NODE* head) { NODE* helper = head->next; head->next = NULL; while (helper != NULL) { helper->next = head; head = helper; helper = helper->next; } } You'd have to double check, though, as I haven't even tested it.
i just got an infinite loop :(
Try and make sure your compiler understand what "NULL" means; it sometimes has a different name.
my compiler has it mean (void*) 0
And I found a logical mistake in mine. :) Quick fix, though. I hope I helped in some way.
You did; thanks to you I finally understand singly linked lists... and the other types of lists are much easier to understand once you get the idea of singly-linked lists.
I'm no longer a loser who thinks linked lists == arrays!
haha
This may be too late to be of interest, but here it is....
Great recursive solution!
without recursion /* reverse a linked list. return a pointer to the new first node */ nodetype* revnodes2(nodetype *p) { nodetype *prev,*pn; if(p) { for(prev= (nodetype *)NULL,pn= p->next; pn; prev=p,p=pn,pn=pn->next) p->next= prev; p->next= prev; } return p; }
Join our real-time social learning platform and learn together with your friends!