
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Middle Element of a Linked List in JavaScript
For a given linked list, write a program in JavaScript to find its middle element. Here, linked lists are a type of linear data structure.
If there are an even number of elements in the linked list, there will be two middle nodes. In this case, the middle element would be the latter element out of both elements.
Example Scenario:
Input: linked_list = 1->2->3->4->5->6-> null Output: middle_element = 4
Using Iteration
Iterate over the whole list and count the number of nodes. Now, iterate over the node till count/2 and return the count/2 i.e. the middle element.
Example
The following JavaScript program shows how to find the middle element of a linked list.
<html> <head> <title>Middle Element of Linked List</title> </head> <body> <script> class Node { constructor(val) { this.data = val; this.next = null; } } class LinkedList { constructor() { this.head = null; } // to add a new node push(new_data) { let new_node = new Node(new_data); if (!this.head) { this.head = new_node; } else { let current = this.head; while (current.next) { current = current.next; } current.next = new_node; } } // counting nodes getCount() { let count = 0; let current = this.head; while (current) { count++; current = current.next; } return count; } // for getting middle node printMiddle() { let count = this.getCount(); let mid = Math.floor(count / 2); let current = this.head; for (let i = 0; i < mid; i++) { current = current.next; } return current.data; } } const linkedlist = new LinkedList(); linkedlist.push('Node 12'); linkedlist.push('Node 24'); linkedlist.push('Node 48'); linkedlist.push('Node 95'); linkedlist.push('Node 56'); document.write('The middle element is: ' + linkedlist.printMiddle()); </script> </body> </html>
On running this code, it will produce the following output ?
The middle element is: Node 48
Using Two Pointers
Traverse the linked list using 2 pointers i.e. slow and fast pointer. Move the slow pointer one node at a time and the fast pointer two nodes at once until the fast pointer points to null. When the fast pointer reaches the end slow pointer will point to the middle element.
Example
In the below example, we are going to find the middle element of the linked list in JavaScript using the two pointers approach.
<html> <head> <title>Middle Element of Linked List</title> </head> <body> <script> // Defining the head pointer var head; /* Linked list node */ class Node { constructor(val) { this.data = val; this.next = null; } } /* Function to print middle of linked list */ function printMiddle(){ var slow_ptr = head; var fast_ptr = head; if (head != null){ while (fast_ptr != null && fast_ptr.next != null){ fast_ptr = fast_ptr.next.next; slow_ptr = slow_ptr.next; } document.write( "The middle element is [" + slow_ptr.data + "] <br/><br/>" ); } } /* Inserts a new Node at front of the list. */ function push(new_data) { /* * 1 & 2: Allocate the Node & Put in the data */ var new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } /* * This function prints contents of linked list starting from the given node */ function printList() { var tnode = head; while (tnode != null) { document.write(tnode.data + "->"); tnode = tnode.next; } document.write("NULL<br/>"); } for (i = 4; i > 0; --i) { push(i); printList(); printMiddle(); } </script> </body> </html>
It will produce the following output ?
5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4]