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.

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

Method 1:
Traverse 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.

Method 2:
Traverse 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.

Java Programming:

[pastacode lang=”java” manual=”%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–i)%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” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

5->NULL
The middle element is [5]

4->5->NULL
The middle element is [5]

3->4->5->NULL
The middle element is [4]

2->3->4->5->NULL
The middle element is [4]

1->2->3->4->5->NULL
The middle element is [3]

Method 3:
Initialize 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.
Thanks to Narendra Kangralkar for suggesting this method.

C Programming:

[pastacode lang=”c” manual=”%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’count’%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–)%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” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

5->NULL
The middle element is [5]

4->5->NULL
The middle element is [5]

3->4->5->NULL
The middle element is [4]

2->3->4->5->NULL
The middle element is [4]

1->2->3->4->5->NULL
The middle element is [3]