def test_reverseBetween5(): head = ListNode.stringToListNode("[1,2,3,4,5,6,7,8,9]") m = 1 n = 5 expect = "[5, 4, 3, 2, 1, 6, 7, 8, 9]" actual = ListNode.listNodeToString(solution.reverseBetween(head, m, n)) assert actual == expect
def test_reverseBetween1(): head = ListNode.stringToListNode("[1,2,3,4,5]") m = 2 n = 4 expect = "[1, 4, 3, 2, 5]" actual = ListNode.listNodeToString(solution.reverseBetween(head, m, n)) assert actual == expect
def deleteDuplicates(self, head: ListNode) -> ListNode: dummy = ListNode(0) prev = dummy dummy.next = head while head and head.next: if head.val == head.next.val: while head.next and head.val == head.next.val: head = head.next head = head.next prev.next = head else: prev = prev.next head = head.next return dummy.next
def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: if m == n: return head n -= m - 1 dummy = ListNode(0) tail1, dummy.next = dummy, head for _ in range(m - 1): tail1, head = head, head.next tail2, head, tail1.next = head, head.next, head for _ in range(n - 1): head.next, tail1.next, head = tail1.next, head, head.next tail2.next = head return dummy.next
def partition(self, head: ListNode, x: int) -> ListNode: node = head head = ListNode(0) prevTail = head pivot = ListNode(0) postTail = pivot while node: if node.val >= x: postTail.next = node postTail = node else: prevTail.next = node prevTail = node node = node.next prevTail.next = pivot.next postTail.next = None return head.next
def insertionSortList(self, head: ListNode) -> ListNode: dummy = ListNode(0) dummy.next = head prev = dummy current = dummy.next while current: nprev = dummy node = nprev.next while node != current and node.val <= current.val: nprev = node node = node.next if node != current: prev.next = current.next current.next = node nprev.next = current else: prev = current current = prev.next return dummy.next
def merge(self, head1, head2): dummy = ListNode(None) tail = dummy while head1 and head2: if head1.val < head2.val: tail.next = head1 tail = tail.next head1 = head1.next else: tail.next = head2 tail = tail.next head2 = head2.next tail.next = head1 or head2 return dummy.next
def test_insertionsortlist1(): head = ListNode.stringToListNode("[4,2,1,3]") expexct = ListNode.stringToListNode("[1,2,3,4]") actual = solution.insertionSortList(head) assert ListNode.listNodeToString(actual) == ListNode.listNodeToString( expexct)
def test_rotateRight1(): head = ListNode.stringToListNode("[1, 2, 3, 4, 5]") k = 2 actual = solution.rotateRight(head, k) expect = ListNode.stringToListNode("[4, 5, 1, 2, 3]") assert ListNode.listNodeToString(actual) == ListNode.listNodeToString(expect)
def test_rotateRight4(): head = ListNode.stringToListNode("[0, 1, 2]") k = 3 actual = solution.rotateRight(head, k) expect = ListNode.stringToListNode("[0, 1, 2]") assert ListNode.listNodeToString(actual) == ListNode.listNodeToString(expect)
def test_reorderList1(): head = ListNode.stringToListNode("[1,2,3,4]") expect = ListNode.stringToListNode("[1,4,2,3]") solution.reorderList(head)
def test_reorderList5(): head = ListNode.stringToListNode("[1,2]") expect = ListNode.stringToListNode("[1,2]") solution.reorderList(head)
def test_deleteDuplicates8(): head = ListNode.stringToListNode('[1, 1, 2, 2]') expect = '[]' actual = ListNode.listNodeToString(solution.deleteDuplicates(head)) assert actual == expect
def test_partitionList1(): head = ListNode.stringToListNode('[1, 4, 3, 2, 5, 2]') x = 3 expect = '[1, 2, 2, 4, 3, 5]' actual = ListNode.listNodeToString(solution.partition(head, x)) assert actual == expect
def test_detectCycle1(): head = ListNode.stringToListNode("[3,2,0,-4]") expect = head.next expect.next.next.next = expect actual = solution.detectCycle(head) assert actual == expect
def test_detectCycle4(): head = ListNode.stringToListNode("[1, 2, 3, 4]") expect = None actual = solution.detectCycle(head) assert actual == expect
def test_detectCycle2(): head = ListNode.stringToListNode("[1, 2]") expect = head expect.next.next = expect actual = solution.detectCycle(head) assert actual == expect
def test_buildTree1(): head = ListNode.stringToListNode("[-10,-3,0,5,9]") expect = TreeNode.stringToTreeNode("[0,-3,9,-10,null,5]") actual = solution.sortedListToBST(head) assert TreeNode.isSame(actual, expect)