
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding middlemost node of a linked list in JavaScript
Problem
We are required to write a JavaScript function that takes in the head of a linked list as the first and the only argument.
Our function should return the value stored in the middlemost node of the list. And if there are two middlemost nodes, we should return the second one of them.
For example, if the list is like this:
Input
[4, 6, 8, 9, 1]
Output
const output = 8;
Following is the code:
Example
class Node { constructor(data) { this.data = data; this.next = null; }; }; class LinkedList { constructor() { this.head = null; this.size = 0; }; }; LinkedList.prototype.add = function(data) { const newNode = new Node(data); let curr; if(this.head === null) { this.head = newNode; } else { curr = this.head; while(curr.next) { curr = curr.next; } curr.next = newNode; }; this.size++; }; const list = new LinkedList(); list.add(4); list.add(6); list.add(8); list.add(9); list.add(1); const findMiddle = (head) => { let slow = head let fast = head while(fast && fast.next) { slow = slow.next fast = fast.next.next } return slow.data }; console.log(findMiddle(list.head));
Output
8
- Related Articles
- Finding middlemost element(s) in JavaScript
- JavaScript Program for Inserting a Node in a Linked List
- JavaScript Program for Finding Length of a Linked List
- JavaScript Program for Finding the Length of Loop in Linked List
- Delete Node in a Linked List in Python
- Linked List Random Node in C++
- Reverse each word in a linked list Node
- Find modular node in a linked list in C++
- JavaScript Program for Writing A Function To Get Nth Node In A Linked List
- Delete a node in a Doubly Linked List in C++
- Finding next greater node for each node in JavaScript
- C# Program to add a node after the given node in a Linked List
- C# Program to add a node before the given node in a Linked List
- Finding Median in a Sorted Linked List in C++
- C# program to find node in Linked List

Advertisements