{"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->2->3->4->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->2->3->4->5->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[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20find%20middle%20of%20linked%20list%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2F%20head%20of%20linked%20list%0A%20%0A%20%20%20%20%2F*%20Linked%20list%20node%20*%2F%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20print%20middle%20of%20linked%20list%20*%2F%0A%20%20%20%20void%20printMiddle()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20slow_ptr%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20Node%20fast_ptr%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20if%20(head%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(fast_ptr%20!%3D%20null%20%26%26%20fast_ptr.next%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20fast_ptr%20%3D%20fast_ptr.next.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slow_ptr%20%3D%20slow_ptr.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22The%20middle%20element%20is%20%5B%22%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20slow_ptr.data%20%2B%20%22%5D%20%5Cn%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Put%20in%20the%20data*%2F%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20This%20function%20prints%20contents%20of%20linked%20list%0A%20%20%20%20%20%20%20starting%20from%20%20the%20given%20node%20*%2F%0A%20%20%20%20public%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20tnode%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(tnode%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(tnode.data%2B%22-%3E%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20tnode%20%3D%20tnode.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println(%22NULL%22)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%20%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D5%3B%20i%3E0%3B%20\u2013i)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20llist.push(i)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20llist.printMiddle()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Rajat%20Mishra\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Output:<\/strong><\/p>\n<pre>5->NULL\r\nThe middle element is [5]\r\n\r\n4->5->NULL\r\nThe middle element is [5]\r\n\r\n3->4->5->NULL\r\nThe middle element is [4]\r\n\r\n2->3->4->5->NULL\r\nThe middle element is [4]\r\n\r\n1->2->3->4->5->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->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[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%0A%2F*%20Function%20to%20get%20the%20middle%20of%20the%20linked%20list*%2F%0Avoid%20printMiddle(struct%20node%20*head)%0A%7B%0A%20%20%20%20int%20count%20%3D%200%3B%0A%20%20%20%20struct%20node%20*mid%20%3D%20head%3B%0A%20%0A%20%20%20%20while%20(head%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20update%20mid%2C%20when%20\u2019count\u2019%20is%20odd%20number%20*%2F%0A%20%20%20%20%20%20%20%20if%20(count%20%26%201)%0A%20%20%20%20%20%20%20%20%20%20%20%20mid%20%3D%20mid-%3Enext%3B%0A%20%0A%20%20%20%20%20%20%20%20%2B%2Bcount%3B%0A%20%20%20%20%20%20%20%20head%20%3D%20head-%3Enext%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20if%20empty%20list%20is%20provided%20*%2F%0A%20%20%20%20if%20(mid%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20printf(%22The%20middle%20element%20is%20%5B%25d%5D%5Cn%5Cn%22%2C%20mid-%3Edata)%3B%0A%7D%0A%20%0A%20%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20a%20given%20linked%20list%0Avoid%20printList(struct%20node%20*ptr)%0A%7B%0A%20%20%20%20while%20(ptr%20!%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22%25d-%3E%22%2C%20ptr-%3Edata)%3B%0A%20%20%20%20%20%20%20%20ptr%20%3D%20ptr-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20printf(%22NULL%5Cn%22)%3B%0A%7D%0A%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F*%20Start%20with%20the%20empty%20list%20*%2F%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%0A%20%20%20%20int%20i%3B%0A%20%0A%20%20%20%20for%20(i%3D5%3B%20i%3E0%3B%20i\u2013)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20push(%26head%2C%20i)%3B%0A%20%20%20%20%20%20%20%20printList(head)%3B%0A%20%20%20%20%20%20%20%20printMiddle(head)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>Output:<\/strong><\/p>\n<pre>5->NULL\r\nThe middle element is [5]\r\n\r\n4->5->NULL\r\nThe middle element is [5]\r\n\r\n3->4->5->NULL\r\nThe middle element is [4]\r\n\r\n2->3->4->5->NULL\r\nThe middle element is [4]\r\n\r\n1->2->3->4->5->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}]}}