Question

You are given the heads of two sorted linked lists list1 and list2.

Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.

Return the head of the merged linked list.

This is alinked_list question.

Idea

  • Have pointer to heads of both linked lists
  • Have dummy pointer
  • While one of the heads are not none
    • Compare magnitudes
    • Append smaller node to dummy pointer
    • Move head of smaller node to the next nod
  • If either list still points to a node, append it to the dummy
  • Return dummy

Solution