{"id":27005,"date":"2018-01-02T20:38:10","date_gmt":"2018-01-02T15:08:10","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27005"},"modified":"2018-01-02T20:38:10","modified_gmt":"2018-01-02T15:08:10","slug":"java-algorithm-find-middle-given-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-find-middle-given-linked-list\/","title":{"rendered":"Java 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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to find middle of linked list<br\/>class LinkedList<br\/>{<br\/>    Node head; \/\/ head of linked list<br\/> <br\/>    \/* Linked list node *\/<br\/>    class Node<br\/>    {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d)<br\/>        {<br\/>            data = d;<br\/>            next = null;<br\/>        }<br\/>    }<br\/> <br\/>    \/* Function to print middle of linked list *\/<br\/>    void printMiddle()<br\/>    {<br\/>        Node slow_ptr = head;<br\/>        Node fast_ptr = head;<br\/>        if (head != null)<br\/>        {<br\/>            while (fast_ptr != null &amp;&amp; fast_ptr.next != null)<br\/>            {<br\/>                fast_ptr = fast_ptr.next.next;<br\/>                slow_ptr = slow_ptr.next;<br\/>            }<br\/>            System.out.println(&quot;The middle element is [&quot; +<br\/>                                slow_ptr.data + &quot;] \\n&quot;);<br\/>        }<br\/>    }<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/> <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/* This function prints contents of linked list<br\/>       starting from  the given node *\/<br\/>    public void printList()<br\/>    {<br\/>        Node tnode = head;<br\/>        while (tnode != null)<br\/>        {<br\/>            System.out.print(tnode.data+&quot;-&gt;&quot;);<br\/>            tnode = tnode.next;<br\/>        }<br\/>        System.out.println(&quot;NULL&quot;);<br\/>    }<br\/> <br\/>    public static void main(String [] args)<br\/>    {<br\/>        LinkedList llist = new LinkedList();<br\/>        for (int i=5; i&gt;0; --i)<br\/>        {<br\/>            llist.push(i);<br\/>            llist.printList();<br\/>            llist.printMiddle();<br\/>        }<br\/>    }<br\/>}<br\/>\/\/ This code is contributed by Rajat Mishra<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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<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[ad type=&#8221;banner&#8221;]\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","protected":false},"excerpt":{"rendered":"<p>Java 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":[79476,79478],"tags":[80603,80604,80598,80601,80602,80600,80599,80597,80605],"class_list":["post-27005","post","type-post","status-publish","format-standard","hentry","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\/27005","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=27005"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27005\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}