Difference between revisions of "TADM2E 3.23"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
(Recovering wiki)
Line 1: Line 1:
 
== Iterative version with three variables ==
 
== Iterative version with three variables ==
<pre>
+
<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-&gt;pNext; // save the next ptr in temp2
+
         temp2= temp1->pNext; // save the next ptr in temp2
         temp1-&gt;pNext = temp3; // change next to privous
+
         temp1->pNext = temp3; // change next to privous
 
         temp3 = temp1;
 
         temp3 = temp1;
 
         temp1 = temp2;
 
         temp1 = temp2;
Line 16: Line 16:
 
   return *List;
 
   return *List;
 
}
 
}
&lt;/pre &gt;
+
</pre >
  
 
== Iterative version with one variable ==
 
== Iterative version with one variable ==
  
&lt;pre&gt;
+
<pre>
 
Node* reverse (node* head)
 
Node* reverse (node* head)
 
{
 
{
Line 26: Line 26:
 
     while(true)
 
     while(true)
 
     {
 
     {
         if(head-&gt;next==NULL)
+
         if(head->next==NULL)
 
         {
 
         {
             head-&gt;next=temp;
+
             head->next=temp;
 
             break;
 
             break;
 
         }
 
         }
         // Swap(head-&gt;next, temp)
+
         // Swap(head->next, temp)
         head-&gt;next = (node *)((unsigned long) head-&gt;next ^(unsigned long) temp);
+
         head->next = (node *)((unsigned long) head->next ^(unsigned long) temp);
         temp = (node *)((unsigned long) head-&gt;next ^(unsigned long) temp);
+
         temp = (node *)((unsigned long) head->next ^(unsigned long) temp);
         head-&gt;next =(node *) ((unsigned long) head-&gt;next ^ (unsigned long)temp);
+
         head->next =(node *) ((unsigned long) head->next ^ (unsigned long)temp);
 
          
 
          
 
         // Swap(head, temp)
 
         // Swap(head, temp)
Line 43: Line 43:
 
     }
 
     }
 
}
 
}
&lt;/pre&gt;
+
</pre>
  
  
 
== Recursive Version ==
 
== Recursive Version ==
  
&lt;pre&gt;
+
<pre>
 
Node* Reverse(Node*head)
 
Node* Reverse(Node*head)
 
{
 
{
Line 56: Line 56:
 
       return NULL;
 
       return NULL;
 
   }
 
   }
   if(head-&gt;next==NULL)
+
   if(head->next==NULL)
 
       return head;
 
       return head;
   temp=Reverse(head-&gt;next);
+
   temp=Reverse(head->next);
   head-&gt;next-&gt;next=head;
+
   head->next->next=head;
   head-&gt;next=NULL;
+
   head->next=NULL;
 
   return temp;
 
   return temp;
 
}
 
}
&lt;/pre&gt;
+
</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)