Beispiel #1
0
def test3(soln):
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    k = 3
    print "Original LL: {0}, K: {1}".format(head, k)
    print soln.reverseKGroup(head, k)
def test3(soln):
    head = ListNode(1)
    head.next = ListNode(2)
    head.next.next = ListNode(3)
    k = 3
    print "Original LL: {0}, K: {1}".format(head, k)
    print soln.reverseKGroup(head, k)
Beispiel #3
0
def setup_test(test):
    for i, val in enumerate(test):
        test[i] = ListNode(val)

    for i in range(len(test) - 1):
        test[i].next = test[i + 1]
    print "Test setup Complete..."
    print "\t", test[0]
    return test[0]
Beispiel #4
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     if not l1 or not l2:
         return l1 or l2
     if l1.val < l2.val:
         l1.next = self.mergeTwoLists(l1.next, l2)
         return l1
     else:
         l2.next = self.mergeTwoLists(l1, l2.next)
         return l2
Beispiel #5
0
def test_1():
    myList = []
    for i in range(10):
        myList += ListNode(i),

    for i in range(9):
        myList[i].next = myList[i + 1]

    return myList[0]
def test0(soln):
    head = ListNode(1)
    k = 1
    print "Original LL: {0}, K: {1}".format(head, k)
    print soln.reverseKGroup(head, k)
Beispiel #7
0
from data_structures.ListNode import ListNode


class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1 or not l2:
            return l1 or l2
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1, l2.next)
            return l2


if __name__ == '__main__':
    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(4)
    node1.next = node2
    node2.next = node3

    node4 = ListNode(1)
    node5 = ListNode(3)
    node6 = ListNode(4)
    node4.next = node5
    node5.next = node6

    Solution().mergeTwoLists(node1, node4)