Javascript Program For Making Middle Node Head In A Linked List Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples:Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves one at a time and second one moves two at a time. When second pointer reaches end, first reaches middle. We also keep track of previous of first pointer so that we can remove middle node from its current position and can make it head. JavaScript // Javascript program to make middle node // as head of Linked list // Link list node class Node { constructor(val) { this.data = val; this.next = null; } } let head; // Function to get the middle and set at // beginning of the linked list function setMiddleHead() { if (head == null) return; // To traverse list nodes one // by one one_node = head; // To traverse list nodes by // skipping one. two_node = head; // To keep track of previous of middle prev = null; while (two_node != null && two_node.next != null) { // for previous node of middle node prev = one_node; // Move one node each time two_node = two_node.next.next; // Move two node each time one_node = one_node.next; } // Set middle node at head prev.next = prev.next.next; one_node.next = head; head = one_node; } // To insert a node at the beginning of // linked list. function push(new_data) { // Allocate node new_node = new Node(new_data); // Link the old list of the new node new_node.next = head; // Move the head to point to the // new node head = new_node; } // A function to print a given linked list function printList(ptr) { while (ptr != null) { console.log(ptr.data); ptr = ptr.next; } } // Driver function // Create a list of 5 nodes head = null; let i; for (i = 5; i > 0; i--) push(i); console.log(" list before: "); printList(head); setMiddleHead(); console.log(" list After: "); printList(head); // This code is contributed by aashish1995 Output list before: 1 2 3 4 5 list After: 3 1 2 4 5 Complexity Analysis: Time Complexity: O(n) when n is the total no of nodes in a linked listSpace Complexity: O(1) because using constant spacePlease refer complete article on Make middle node head in a linked list for more details! Comment More info K kartik Follow Improve Article Tags : JavaScript Tortoise-Hare-Approach JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like