コード例 #1
0
ファイル: 725. 分隔链表.py プロジェクト: fengges/leetcode
            r[i] += 1
        head = ListNode(0)
        head.next = root
        re = []
        for i in r:
            re.append(head.next)
            temp = head
            for j in range(i):
                head = head.next
            temp.next = None
        return re


s = Solution()

test = [
    {
        "input": [toLinkNode([1, 2, 3]), 5],
        "output": [[1], [2], [3], [], []]
    },
    {
        "input": [toLinkNode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3],
        "output": [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
    },
]

for t in test:
    r = s.splitListToParts(t['input'][0], t['input'][1])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
コード例 #2
0
ファイル: 141. 环形链表.py プロジェクト: fengges/leetcode
    def hasCycle(self, head):
        node1 = head
        node2 = head
        while node1 is not None and node2 is not None:
            node1 = node1.next
            if node2.next is None:
                node2 = node2.next
                break
            node2 = node2.next.next
            if node1 is not None and node1 == node2:
                break
        if node1 is None or node2 is None:
            return False
        else:
            return True


s = Solution()

test = [
    {
        "input": toLinkNode([1, 2, 3, 4, 5]),
        "output": True
    },
]

for t in test:
    r = s.hasCycle(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
        # r = s.reverseBetween(t['input'][0], t['input'][1], t['input'][2])
コード例 #3
0
ファイル: 817. 链表组件.py プロジェクト: fengges/leetcode
                if find:
                    r += 1
                find = False
            else:
                find = True
            head = head.next
        if find:
            r += 1
        return r


s = Solution()

test = [
    {
        "input": [toLinkNode([0, 1, 2]), [1, 0]],
        "output": 1
    },
    {
        "input": [toLinkNode([0, 1, 2, 3]), [0, 1, 3]],
        "output": 2
    },
    {
        "input": [toLinkNode([0, 1, 2, 3, 4]), [0, 3, 1, 4]],
        "output": 2
    },
]

for t in test:
    r = s.numComponents(t['input'][0], t['input'][1])
    if r != t['output']:
コード例 #4
0
class Solution(object):
    def detectCycle(self, head):
        fast, slow = head, head
        if fast is None or fast.next is None:
            return None
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                break
        if fast is None or fast.next is None:
            return None
        slow = head
        while slow != fast:
            slow = slow.next
            fast = fast.next
        return slow


s = Solution()
test = [
    {
        "input": toLinkNode([1, 2]),
        "output": 26
    },
]

for t in test:
    r = s.detectCycle(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))
コード例 #5
0
        while head:
            s += 1
            if s % 2 == 1:
                l.next = head
                l = head
            else:
                r.next = head
                r = head
            head = head.next
        l.next = r1.next
        r.next = None
        return l1.next


s = Solution()

test = [
    {
        "input": toLinkNode([1, 2, 3, 4, 5]),
        "output": [[1], [2], [3], [], []]
    },
    {
        "input": toLinkNode([2, 1, 3, 5, 6, 4, 7]),
        "output": [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
    },
]

for t in test:
    r = s.oddEvenList(t['input'])
    if r != t['output']:
        print("error:" + str(t) + " out:" + str(r))