예제 #1
0
                        Initiallly:
                            k = 3
                            pre = 4 -> None
                            curr = 1 -> 2 -> 3 -> 4 -> None
                        Round 0:
                            pre = 1 -> 4 -> None
                            curr = 2 -> 3 -> 4 -> None
                        Round 1:
                            pre = 2 -> 1 -> 4 -> None
                            curr = 3 -> 4 -> None
                        Round 2:
                            pre = 3 -> 2 -> 1 -> 4 -> None
                            curr = 4 -> None
                        Now the nodes in the k group is reversed and stored in
                        pre.
                    """
                    temp = curr.next  # Store curr.next for later reference.
                    curr.next = pre  # Pre-append current node to the remain.
                    pre = curr  # Move remain pointer to the current node.
                    curr = temp  # Move current node to the previous next node.

                jump.next = pre  # Append the reversed node list to fakeHead.
                jump = l  # Move jump to the end point of the current k group.
                l = r  # Set l to the first item in the next k group.
            else:  # List is exhausted.
                return fakeHead.next


x = ListNode(None)
print(Solution().reverseKGroup(x.create_node_list(1, 5), 3))
예제 #2
0
                        tempNode.next = l1
                        l1 = l1.next
                    else:
                        tempNode.next = l2
                        l2 = l2.next
                    tempNode = tempNode.next

                if l1:
                    tempNode.next = l1

                if l2:
                    tempNode.next = l2

                lists[i] = fakeHead.next  # Store merged list back to lists[i].

            interval *= 2  # Increase interval.

        return lists[0]


x = ListNode(None)
s = Solution()
l1 = x.create_node_list(givenList=[1, 4, 5])
l2 = x.create_node_list(givenList=[1, 3, 4])
l3 = x.create_node_list(givenList=[2, 6])
print('Original list is: ')
for l in [l1, l2, l3]:
    l.print_node_list()
print('Merged list is:')
s.mergeKLists([l1, l2, l3]).print_node_list()
예제 #3
0
from test_helper import ListNode


class Solution:
    def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
        if not root:
            return [None] * k

        # First calculate the total length of the list.
        currNode, total = root, 0
        while currNode:
            total += 1
            currNode = currNode.next

        partLen, remainLen = divmod(total, k)
        currNode, rslt, partCnt = root, [], 0
        for partCnt in range(k):  # Split list to parts with partLen length.
            rslt.append(currNode)
            for _ in range(partLen + (partCnt < remainLen)):
                preNode, currNode = currNode, currNode.next

            preNode.next = None

        return rslt


root = ListNode(1)
root.create_node_list(1, 11)
print(Solution().splitListToParts(root.next, 3))