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 a linked_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