Singly-linked list/Reversal: Difference between revisions

Added a routine for the language C.
imported>StraightDoubt
(Added a routine for the language C.)
Line 47:
</pre>
 
== C ==
This code works by reversing the pointers in the list. The function returns the new beginning of the list, or NULL if passed a null pointer.
#include <stdlib.h>
struct node {
struct node *next;
int data;
};
struct node *
reverse(struct node *head) {
struct node *prev, *cur, *next;
prev = NULL;
for (cur = head; cur != NULL; cur = next) {
next = cur->next;
cur->next = prev;
prev = cur;
}
return prev;
}
 
=={{header|Delphi}}==