# Merging LinkedLists

* max(m, n), max(m, n)

```jsx
// 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

```jsx
// 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 flag1 = false;
  let flag2 = false;
  
  let node1 = linkedListOne;
  let node2 = linkedListTwo;
  while (node1 !== null || node2 !== null) {
    if (node1 === null) {
      node1 = linkedListTwo;
    }
    if (node2 === null) {
      node2 = linkedListOne;
    }
    if (node1 === node2) return node1;

    node1 = node1.next;
    node2 = node2.next;
  }
  
  return null;
}

// Do not edit the line below.
exports.mergingLinkedLists = mergingLinkedLists;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/merging-linkedlists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
