program to Reverse a linked list
Answers were Sorted based on User's Feedback
Answer / shruti
reverse a linked list -> by creating a new list.
consider home as the stationary pointer of the original list
adn start as the stationary pointer of the new linked list..
the node structure is as follows:
struct node
{
int data;
struct node * next;
}
struct node * reverse(struct node *home , struct node *
start)
temp = home;
while(temp != NULL)
{
p = (struct node *) malloc (sizeof(struct node));
p -> data = temp -> data;
p -> next = NULL;
if(start == NULL)
start = p;
else
{
p -> next = start;
start = p;
}
temp = temp -> next;
return start;
}
Is This Answer Correct ? | 42 Yes | 16 No |
Answer / gaurav jain
void reverse(struct node **kbr)
{
struct node *temp,*p,*q;
temp=*kbr;
p=temp->next;
q=temp;
while(p!=NULL)
{
temp=p;
p=p->next;
temp->next=q;
q=temp;
}
(*kbr)->next=NULL;
*kbr=q;
}
Is This Answer Correct ? | 24 Yes | 16 No |
Answer / ashish rajta
void reverse(list *l)
{
lnode *temp;
temp=l->first;
while(temp->next != NULL)
{
temp=temp->next;
}
while(temp !=NULL)
{
printf("%d",temp->info);
temp=temp->prev;
}
}
Is This Answer Correct ? | 15 Yes | 12 No |
Answer / ajay george
void rev(node *nPtr, node *prevPtr)
{
if(nPtr->next==NULL)
{
head=nPtr;
}
else
{
rev(nPtr->next, nPtr);
}
nPtr->next=prevPtr;
prevPtr->next=NULL;
}
Is This Answer Correct ? | 3 Yes | 1 No |
Answer / mbm
/* Reverse linked list by recursion */
if(head)
head = _ReverseLinkedList(NULL, head, head->next);
NodeStr *_ReverseLinkedList(
NodeStr *preNode,
NodeStr *node1,
NodeStr *node2
)
{
NodeStr *next_node;
if(!node2)
return node1;
next_node = node2->next;
node2->next = node1;
node1->next = preNode;
return _ReverseLinkedList(node1, node2, next_node);
}
Is This Answer Correct ? | 11 Yes | 11 No |
Answer / raj
void linkreverse()
{
node *p=head,*q=head->next,*r=q->next,*temp;
p->next=NULL;
temp=p;
while(p!=tail)
{
q->next=p;
p=q;
if(r!=NULL)
{
q=r;
r=q->next;
}
}
head=p;
tail=temp;
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / cleonjoys
void reverse(Node_t *nPtr, Node_t *prevPtr){
if(nPtr->next != NULL){
reverse(nPtr->next, nPtr);
}
else{
headp = nPtr; //header is a global pointer, i know
keeping header is security hole.
}
nPtr->next = prevPtr;
}
Is This Answer Correct ? | 7 Yes | 8 No |
Answer / bragaadeesh
Program to reverse a singly list ITERATIVELY,
http://www.technicalypto.com/2010/01/java-program-to-reverse-singly-linked.html
Program to reverse a linked list RECURSIVELY
http://www.technicalypto.com/2010/03/reverse-singly-linked-list-recursively.html
Is This Answer Correct ? | 4 Yes | 7 No |
Answer / don
int rec_rev(node *p) {
node *last_prev,*last,*temp,*q;
if (p->next==NULL)
return 1;
else {
last_prev=find_last(); // finds last but 1 element
last=last_prev->next;
last_prev->next==NULL;
if(p==head) {
temp=head;
head=last;
head->next=temp;
p=temp;
rec_rev(p);
}
else {
q=find_prev(p);
last->next=q->next;
q->next=last;
q->next->next=p;
rec_rev(p);
}
}
]
Is This Answer Correct ? | 3 Yes | 7 No |
What are the files which are automatically opened when a C file is executed?
abcdedcba abc cba ab ba a a
main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }
Is the following code legal? struct a { int x; struct a b; }
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.
to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD
#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }
why java is platform independent?
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
source code for delete data in array for c
main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }
int a=1; printf("%d %d %d",a++,a++,a); need o/p in 'c' and what explanation too