{"id":27004,"date":"2018-01-02T20:39:14","date_gmt":"2018-01-02T15:09:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27004"},"modified":"2018-01-02T20:39:14","modified_gmt":"2018-01-02T15:09:14","slug":"find-middle-given-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/find-middle-given-linked-list\/","title":{"rendered":"C Algorithm &#8211; Find the middle of a given linked list"},"content":{"rendered":"<p>Given a singly linked list, find middle of the linked list. For example, if given linked list is 1-&gt;2-&gt;3-&gt;4-&gt;5 then output should be 3.<\/p>\n<p>If there are even nodes, then there would be two middle nodes, we need to print second middle element. For example, if given linked list is 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6 then output should be 4<\/p>\n<p><strong>Method 1:<\/strong><br \/>\nTraverse the whole linked list and count the no. of nodes. Now traverse the list again till count\/2 and return the node at count\/2.<br \/>\n<span id=\"more-821\"><\/span><\/p>\n<p><strong>Method 2:<\/strong><br \/>\nTraverse linked list using two pointers. Move one pointer by one and other pointer by two. When the fast pointer reaches end slow pointer will reach middle of the linked list.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <br\/>\/* Function to get the middle of the linked list*\/<br\/>void printMiddle(struct node *head)<br\/>{<br\/>    struct node *slow_ptr = head;<br\/>    struct node *fast_ptr = head;<br\/> <br\/>    if (head!=NULL)<br\/>    {<br\/>        while (fast_ptr != NULL &amp;&amp; fast_ptr-&gt;next != NULL)<br\/>        {<br\/>            fast_ptr = fast_ptr-&gt;next-&gt;next;<br\/>            slow_ptr = slow_ptr-&gt;next;<br\/>        }<br\/>        printf(&quot;The middle element is [%d]\\n\\n&quot;, slow_ptr-&gt;data);<br\/>    }<br\/>}<br\/> <br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>        (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/\/ A utility function to print a given linked list<br\/>void printList(struct node *ptr)<br\/>{<br\/>    while (ptr != NULL)<br\/>    {<br\/>        printf(&quot;%d-&gt;&quot;, ptr-&gt;data);<br\/>        ptr = ptr-&gt;next;<br\/>    }<br\/>    printf(&quot;NULL\\n&quot;);<br\/>}<br\/> <br\/>\/* Drier program to test above function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>    int i;<br\/> <br\/>    for (i=5; i&gt;0; i--)<br\/>    {<br\/>        push(&amp;head, i);<br\/>        printList(head);<br\/>        printMiddle(head);<br\/>    }<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>5-&gt;NULL\r\nThe middle element is [5]\r\n\r\n4-&gt;5-&gt;NULL\r\nThe middle element is [5]\r\n\r\n3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [4]\r\n\r\n2-&gt;3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [4]\r\n\r\n1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [3]<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 3:<\/strong><br \/>\nInitialize mid element as head and initialize a counter as 0. Traverse the list from head, while traversing increment the counter and change mid to mid-&gt;next whenever the counter is odd. So the mid will move only half of the total length of the list.<br \/>\nThanks to Narendra Kangralkar for suggesting this method.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/> <br\/>\/* Link list node *\/<br\/>struct node<br\/>{<br\/>    int data;<br\/>    struct node* next;<br\/>};<br\/> <br\/>\/* Function to get the middle of the linked list*\/<br\/>void printMiddle(struct node *head)<br\/>{<br\/>    int count = 0;<br\/>    struct node *mid = head;<br\/> <br\/>    while (head != NULL)<br\/>    {<br\/>        \/* update mid, when &#039;count&#039; is odd number *\/<br\/>        if (count &amp; 1)<br\/>            mid = mid-&gt;next;<br\/> <br\/>        ++count;<br\/>        head = head-&gt;next;<br\/>    }<br\/> <br\/>    \/* if empty list is provided *\/<br\/>    if (mid != NULL)<br\/>        printf(&quot;The middle element is [%d]\\n\\n&quot;, mid-&gt;data);<br\/>}<br\/> <br\/> <br\/>void push(struct node** head_ref, int new_data)<br\/>{<br\/>    \/* allocate node *\/<br\/>    struct node* new_node =<br\/>        (struct node*) malloc(sizeof(struct node));<br\/> <br\/>    \/* put in the data  *\/<br\/>    new_node-&gt;data  = new_data;<br\/> <br\/>    \/* link the old list off the new node *\/<br\/>    new_node-&gt;next = (*head_ref);<br\/> <br\/>    \/* move the head to point to the new node *\/<br\/>    (*head_ref)    = new_node;<br\/>}<br\/> <br\/>\/\/ A utility function to print a given linked list<br\/>void printList(struct node *ptr)<br\/>{<br\/>    while (ptr != NULL)<br\/>    {<br\/>        printf(&quot;%d-&gt;&quot;, ptr-&gt;data);<br\/>        ptr = ptr-&gt;next;<br\/>    }<br\/>    printf(&quot;NULL\\n&quot;);<br\/>}<br\/> <br\/>\/* Drier program to test above function*\/<br\/>int main()<br\/>{<br\/>    \/* Start with the empty list *\/<br\/>    struct node* head = NULL;<br\/>    int i;<br\/> <br\/>    for (i=5; i&gt;0; i--)<br\/>    {<br\/>        push(&amp;head, i);<br\/>        printList(head);<br\/>        printMiddle(head);<br\/>    }<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>5-&gt;NULL\r\nThe middle element is [5]\r\n\r\n4-&gt;5-&gt;NULL\r\nThe middle element is [5]\r\n\r\n3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [4]\r\n\r\n2-&gt;3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [4]\r\n\r\n1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL\r\nThe middle element is [3]<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Find the middle of a given linked list &#8211; Linked List &#8211; Given a singly linked list, find middle of the linked list<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1,79476,79478],"tags":[80603,80604,80598,80601,80602,80600,80599,80597,80605],"class_list":["post-27004","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","category-linked-list","category-singly-linked-list","tag-find-middle-element-in-array","tag-find-middle-node-in-doubly-linked-list","tag-how-to-find-3rd-element-from-end-in-a-linked-list-in-one-pass","tag-how-to-find-a-loop-in-the-singly-linked-list","tag-how-to-find-if-linked-list-has-a-loop","tag-how-to-find-middle-element-of-linked-list-in-c","tag-how-to-find-middle-element-of-linked-list-in-one-pass-in-c","tag-how-to-find-middle-element-of-linked-list-in-one-pass-in-java","tag-write-a-program-to-find-the-middle-element-of-the-linked-list-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27004"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27004\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}