Difference between revisions of "TADM2E 3.20"

From Algorithm Wiki
Jump to: navigation, search
(Recovering wiki)
 
m
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
Node* find_middle(Node *top) {
+
  Node* find_middle(Node *top) {
 
     int k=0;
 
     int k=0;
 
     Node *middle=top, *cur=top;
 
     Node *middle=top, *cur=top;
 
     while (cur) {
 
     while (cur) {
         cur = cur->next;
+
         cur = cur->next;
 
         ++k;
 
         ++k;
 
         if (k%2 == 0)
 
         if (k%2 == 0)
             middle = middle->next;
+
             middle = middle->next;
 
     }
 
     }
 
     return middle;
 
     return middle;

Latest revision as of 10:30, 4 May 2018

 Node* find_middle(Node *top) {
   int k=0;
   Node *middle=top, *cur=top;
   while (cur) {
       cur = cur->next;
       ++k;
       if (k%2 == 0)
           middle = middle->next;
   }
   return middle;
 }

[Also, can be done by improving one pointer two and one pointer one node(s) at a time. When the two node pointer reaches end, the one node pointer is in the middle.]