Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Print middle N nodes of linked list

Suppose linked list contain following nodes [ 1, 2, 3, 4, 5, 6, 7, 8 ,9, 10 ]. Our goal is to Print middle N nodes of linked list.

Test Case

The following cases are follow of this problem.

Case 1 : first and last node are not a part of middle nodes. Example linked list contain 3 nodes [1,2,3]. and there are only one middle [2]. if user are given to print 2 middle node of linked list then it is not possible.

Case 2 : if two set of middle node then print first set of linked list. example [1,2,3,4,5] if print 2 middle nodes. then possible to sets A [2,3] and B[3,4]. in this case print first set.

root_ptr10987654321

Try it Yourself

View pointers and nodes.

Stack Areamainprint_node (int) ? root(pointer) Heap Areastruct Nodedata (int)= 1next (pointer) struct Nodedata (int)= 2next (pointer) struct Nodedata (int)= 3next (pointer) struct Nodedata (int)= 4next (pointer) struct Nodedata (int)= 5next (pointer) struct Nodedata (int)= 6next (pointer) struct Nodedata (int)= 7next (pointer) struct Nodedata (int)= 8next (pointer) struct Nodedata (int)= 9next (pointer) struct Nodedata (int)= 10next (pointer)= NULL

Try it Yourself

Example : print middle 6 nodes.

root_ptr10987654321Middle [6] nodes is : 3 4 5 6 7 8

Example 2: N=4.

root_ptr10987654321Middle [4] nodes is : 4 5 6 7

Accepted Output

C program to print Middle N nodes of linked list . Iterative approach.


Output

Spread the post

Recommended Posts: