Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Linked List Manipulation in JavaScript: Swapping Nodes, Removing Nth Node from End, and Detecting Cycle Entry

Tech May 17 2

Swapipng Adjacent Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return the modified head. Node values must not be altered—only node references can be changed.

Example:

<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [2,1,4,3]

A dummy head simplifies edge handling, especially for the first pair. The algorithm iteratively swaps pairs using three pointter adjustments per iteration.

var swapPairs = function(head) {
    const dummy = new ListNode(0, head);
    let prev = dummy;

    while (prev.next && prev.next.next) {
        const first = prev.next;
        const second = prev.next.next;

        // Perform the swap
        first.next = second.next;
        second.next = first;
        prev.next = second;

        // Move to next pair
        prev = first;
    }

    return dummy.next;
};

Removing the Nth Node from End

Delete the nth node from the end of the list and return the updated head.

Example:

<strong>Input:</strong> head = [1,2,3,4,5], n = 2
<strong>Output:</strong> [1,2,3,5]

Use two pointers: advance the fast pointer by n + 1 steps so that when both move together, the slow pointer lands just before the target node.

var removeNthFromEnd = function(head, n) {
    const dummy = new ListNode(0, head);
    let fast = dummy;
    let slow = dummy;

    // Advance fast pointer by n+1 steps
    for (let i = 0; i <= n; i++) {
        fast = fast.next;
    }

    // Move both until fast reaches end
    while (fast !== null) {
        fast = fast.next;
        slow = slow.next;
    }

    // Remove the nth node from end
    slow.next = slow.next.next;
    return dummy.next;
};

Finding the Start of a Cycle in a Linked List

Determine the first node where a cycle begins. Return null if no cycle exists.

Example:

<strong>Input:</strong> head = [3,2,0,-4], pos = 1
<strong>Output:</strong> node with value 2

Use Floyd’s cycle detection: a slow pointer (1 step) and fast pointer (2 steps). If they meet, a cycle exists. Then reset one pointer to head and move both at same speed—their meeting point is the cycle entrance.

Mathematical insight: if distance from head to cycle start is x, and from cycle start to meeting point is y, with remaining cycle length z, then x = z + k*(cycle length). Thus, restarting from head and meeting point converges at entry.

var detectCycle = function(head) {
    if (!head || !head.next) return null;

    let slow = head;
    let fast = head;

    // Phase 1: Detect if cycle exists
    while (fast && fast.next) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow === fast) break;
    }

    // No cycle found
    if (!fast || !fast.next) return null;

    // Phase 2: Find cycle entrance
    slow = head;
    while (slow !== fast) {
        slow = slow.next;
        fast = fast.next;
    }

    return slow;
};

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.