Spread the post

Print mid-node of linked list.

Print or find the middle element of a given linked list there are following method.

Method 1: Count all nodes and divide by 2.

Algorithm:

Time complexity of this algorithm is O(n).

Method 2: Used two pointer. one pointer are increment by one , and second pointer are increment by two and get mid node.

Algorithm:

Time complexity of this algorithm is O(n).

Print middle of given linked list. First need to find that particular node. Suppose following data [556, 424, 520, 525, 497, 584, 554, 449, 545, 505] are inserted on linked list.

Given Linked list

Try it yourself

In this given linked list number of nodes are 10. that means two middle node are possible in this linked list at position 5 and 6. Choose any one of them of that position.

Output:

Middle of linked list

Execution process

Code execution process

Try it yourself

c program to print middle of linked list.

OutPut:

This program is work on method 2. Replace this print_middle function to below code then it will work on method 1.

Spread the post

Recommended Posts: