• + 21 comments

    Heres the recursive python 2 / 3 solution I came to.

    def MergeLists(headA, headB):
      if headA is None and headB is None:
        return None
      
      if headA is None:
        return headB
    
      if headB is None:
        return headA
      
      if headA.data < headB.data:
        smallerNode = headA
        smallerNode.next = MergeLists(headA.next, headB)
      else:
        smallerNode = headB
        smallerNode.next = MergeLists(headA, headB.next)
      
      return smallerNode