Difference between revisions of "TADM2E 3.23"
From Algorithm Wiki
(Recovering wiki) |
(Recovering wiki) |
||
Line 1: | Line 1: | ||
== Iterative version with three variables == | == Iterative version with three variables == | ||
− | + | <pre> | |
Node* ReverseList( Node * List ) | Node* ReverseList( Node * List ) | ||
{ | { | ||
Line 9: | Line 9: | ||
{ | { | ||
*List = temp1; //set the head to last node | *List = temp1; //set the head to last node | ||
− | temp2= temp1- | + | temp2= temp1->pNext; // save the next ptr in temp2 |
− | temp1- | + | temp1->pNext = temp3; // change next to privous |
temp3 = temp1; | temp3 = temp1; | ||
temp1 = temp2; | temp1 = temp2; | ||
Line 16: | Line 16: | ||
return *List; | return *List; | ||
} | } | ||
− | + | </pre > | |
== Iterative version with one variable == | == Iterative version with one variable == | ||
− | + | <pre> | |
Node* reverse (node* head) | Node* reverse (node* head) | ||
{ | { | ||
Line 26: | Line 26: | ||
while(true) | while(true) | ||
{ | { | ||
− | if(head- | + | if(head->next==NULL) |
{ | { | ||
− | head- | + | head->next=temp; |
break; | break; | ||
} | } | ||
− | // Swap(head- | + | // Swap(head->next, temp) |
− | head- | + | head->next = (node *)((unsigned long) head->next ^(unsigned long) temp); |
− | temp = (node *)((unsigned long) head- | + | temp = (node *)((unsigned long) head->next ^(unsigned long) temp); |
− | head- | + | head->next =(node *) ((unsigned long) head->next ^ (unsigned long)temp); |
// Swap(head, temp) | // Swap(head, temp) | ||
Line 43: | Line 43: | ||
} | } | ||
} | } | ||
− | + | </pre> | |
== Recursive Version == | == Recursive Version == | ||
− | + | <pre> | |
Node* Reverse(Node*head) | Node* Reverse(Node*head) | ||
{ | { | ||
Line 56: | Line 56: | ||
return NULL; | return NULL; | ||
} | } | ||
− | if(head- | + | if(head->next==NULL) |
return head; | return head; | ||
− | temp=Reverse(head- | + | temp=Reverse(head->next); |
− | head- | + | head->next->next=head; |
− | head- | + | head->next=NULL; |
return temp; | return temp; | ||
} | } | ||
− | + | </pre> | |
--[[User:Max|Max]] 07:36, 16 June 2010 (EDT) | --[[User:Max|Max]] 07:36, 16 June 2010 (EDT) |
Revision as of 18:22, 11 September 2014
Iterative version with three variables
Node* ReverseList( Node * List ) { Node *temp1 = *List; Node * temp2 = NULL; Node * temp3 = NULL; while ( temp1 ) { *List = temp1; //set the head to last node temp2= temp1->pNext; // save the next ptr in temp2 temp1->pNext = temp3; // change next to privous temp3 = temp1; temp1 = temp2; } return *List; }
Iterative version with one variable
Node* reverse (node* head) { node *temp=NULL; while(true) { if(head->next==NULL) { head->next=temp; break; } // Swap(head->next, temp) head->next = (node *)((unsigned long) head->next ^(unsigned long) temp); temp = (node *)((unsigned long) head->next ^(unsigned long) temp); head->next =(node *) ((unsigned long) head->next ^ (unsigned long)temp); // Swap(head, temp) head = (node *)((unsigned long) head ^(unsigned long) temp); temp =(node *) ((unsigned long) head ^(unsigned long) temp); head =(node *) ((unsigned long) head ^ (unsigned long)temp); } }
Recursive Version
Node* Reverse(Node*head) { Node* temp=NULL; if(head==NULL) { return NULL; } if(head->next==NULL) return head; temp=Reverse(head->next); head->next->next=head; head->next=NULL; return temp; }
--Max 07:36, 16 June 2010 (EDT)