Make middle node head in a linked list

Move middle node of linked list to front position, But first we are need to find the middle node.

Suppose we are inserted the following (1,2,3,4,5,6,7) node in a sequence.

Before Make Middle node as head Make middle node as head in a linked list

We can solve this problem in a two ways.

Method 1: This is simplest method first count all linked list nodes from linked list traversal. after that iterates ((count-1)/2) nodes of tree and move middle node to first position. This process time complexity is O(n).

Method 2: In this methode are use two pointer variable that are hold the reference of first node of linked list.

first point are visiting one by one into next upcoming nodes, and second pointer are visiting every second node. Continue this process until second pointer is not NULL or that is not possible to visit that is next upcoming nodes. first pointer are hold the reference of middle pointer and we can split this node of linked list and add this node at first position

Here given code implementation process.



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved