Open In App

Break the Linked List in Two Halves using JavaScript

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A linked list is a linear data structure consisting of nodes, each containing a data element and a reference (or link) to the next node in the sequence. It provides dynamic memory allocation, and easy insertion, and deletion.

Unlike arrays, linked lists do not require contiguous memory and allow efficient data manipulation. Its first node is referred as the HEAD, and its last node will point to NULL, indicating the end of the linked list.

Below are the approaches to break the linked list into two halves:

Using Slow and Fast Pointers

In this approach, we use two pointers – a slow pointer and a fast pointer. The slow pointer traverses one node of the linked list at a time while the fast pointer traverses two nodes of the linked list at a time. When the fast pointer reaches the end of the linked list, the slow pointer will be at the middle node of the linked list. We then break the list into two halves from this middle point.

Example: The below code example Uses the Slow and Fast Pointers Method to break the Linked list in two halves in JavaScript.

Javascript
class ListNode {
    constructor(val, next = null) {
        this.val = val;
        this.next = next;
    }
}

// For creating linked list
function createLinkedList(arr) {
    if (!arr || arr.length === 0) {
        return null;
    }

    let head = new ListNode(arr[0]);
    let current = head;

    for (let i = 1; i < arr.length; i++) {
        current.next = new ListNode(arr[i]);
        current = current.next;
    }

    return head;
}

// For breaking linked list into two
function breakLinkedListApproach1(head) {
    if (!head || !head.next) {
        return [head, null];
    }

    let slow = head;
    let fast = head;
    let prev = null;

    while (fast && fast.next) {
        prev = slow;
        slow = slow.next;
        fast = fast.next.next;
    }

    // Break the list at middle
    prev.next = null;
    return [head, slow];
}

// For printing linked list
function printLinkedList(head) {
    let current = head;
    while (current) {
        console.log(current.val);
        current = current.next;
    }
}

let head = createLinkedList([1, 2, 3, 4, 5, 6]);

let [firstHalfApproach1, secondHalfApproach1] = 
    breakLinkedListApproach1(head);

// Printing the first half
console.log("First Half:");
printLinkedList(firstHalfApproach1);

// Printing the second half
console.log("Second Half:");
printLinkedList(secondHalfApproach1);

Output
First Half:
1
2
3
Second Half:
4
5
6

Counting Nodes and Splitting

In this approach, we first count the total number of nodes in the linked list. Then, we divide this count by 2 to find the middle position. After finding the middle position, we traverse the list again to split it into two halves.

Example: The below code example Uses the Counting Nodes and Splitting Method to break the Linked list in two halves in JavaScript.

Javascript
class ListNode {
    constructor(val, next = null) {
        this.val = val;
        this.next = next;
    }
}

// For creating linked list
function createLinkedList(arr) {
    if (!arr || arr.length === 0) {
        return null;
    }

    let head = new ListNode(arr[0]);
    let current = head;

    for (let i = 1; i < arr.length; i++) {
        current.next = new ListNode(arr[i]);
        current = current.next;
    }

    return head;
}

// For breaking linked list into two
function breakLinkedListApproach2(head) {
    if (!head || !head.next) {
        return [head, null];
    }

    let count = 0;
    let current = head;
    while (current) {
        count++;
        current = current.next;
    }

    let middle = Math.floor(count / 2);
    current = head;
    for (let i = 0; i < middle - 1; i++) {
        current = current.next;
    }

    let secondHalf = current.next;
    current.next = null;

    return [head, secondHalf];
}

// For printing linked list
function printLinkedList(head) {
    let current = head;
    while (current) {
        console.log(current.val);
        current = current.next;
    }
}

let head = createLinkedList([1, 2, 3, 4, 5, 6]);

let [firstHalfApproach2, secondHalfApproach2] = 
    breakLinkedListApproach2(head);

// Printing the first half
console.log("First Half:");
printLinkedList(firstHalfApproach2);

// Printing the second half
console.log("Second Half:");
printLinkedList(secondHalfApproach2);

Output
First Half:
1
2
3
Second Half:
4
5
6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads