Merge two sorted linked lists

  • + 0 comments
    def listToArray(head):
        current = head
        result = []
        while current:
            result.append(current.data) 
            current = current.next 
        return result
      
        
    def sortedArray(arr):
        result = []
        for data in arr:
            inserted = False
            for i in range(len(result)):
                if data < result[i]:
                    result.insert(i, data)
                    inserted = True
                    break
            if not inserted:
                result.append(data)
        return result
        
    def convertList(sorted):
        llist3 = SinglyLinkedList()
        for data in sorted:
            llist3.insert_node(data)
        return llist3.head
    
    def mergeLists(head1, head2):
        arrA = listToArray(head1)
        arrB = listToArray(head2)
        concatArr = arrA+arrB
        sorted = sortedArray(concatArr)
        linkedlist = convertList(sorted)
        return linkedlist