Merging LinkedLists

  • max(m, n), max(m, n)

// This is an input class. Do not edit.
class LinkedList {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

exports.LinkedList = LinkedList;

function mergingLinkedLists(linkedListOne, linkedListTwo) {
  let set = new Set();
  let currentNode = linkedListOne;
  while (currentNode !== null) {
    set.add(currentNode);
    currentNode = currentNode.next;
  }

  currentNode = linkedListTwo;
  while (currentNode !== null) {
    if (set.has(currentNode)) {
      return currentNode;
    }
    currentNode = currentNode.next;
  }
  
  return null;
}

// Do not edit the line below.
exports.mergingLinkedLists = mergingLinkedLists;
  • max(n, m), 1

Last updated