TADM2E 3.20

From Algorithm Wiki
Revision as of 18:13, 11 September 2014 by Algowikiadmin (talk | contribs) (Recovering wiki)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.]