LeetCode solutions

117. Populating Next Right Pointers in Each Node II

My accepted JavaScript solution to LeetCode problem 117, Populating Next Right Pointers in Each Node II, running in 47ms.

  • Difficulty: Medium
  • JavaScript
  • Runtime 47ms
  • Memory 58.3MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 47ms, memory 58.3MB, accepted 2025-12-24.

javascript
/**
 * @param {Node} root
 * @return {Node}
 */
var connect = function(root) {
    if (!root) return null;
    
    let curr = root;
    
    while (curr) {
        let dummy = new Node(0);
        let tail = dummy;
        
        // Process current level
        while (curr) {
            if (curr.left) {
                tail.next = curr.left;
                tail = tail.next;
            }
            if (curr.right) {
                tail.next = curr.right;
                tail = tail.next;
            }
            curr = curr.next;
        }
        
        // Move to next level
        curr = dummy.next;
    }
    
    return root;
};

Source