示例#1
0
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # 反转链表
        p = slow.next
        slow.next = None
        if p is None: return
        pre = None
        while p:
            aft = p.next
            p.next = pre
            pre = p
            p = aft
        # 合并链表
        a, b = head, pre
        while True:
            an = a.next
            bn = b.next if b else None
            a.next = b
            if b:
                b.next = an
            a = an
            b = bn
            if not b:
                break


node = ListNode.fromList([1])
Solution().reorderList(node)
print(node)
示例#2
0
                for _ in range(sub_len - 1):
                    if cur and cur.next:
                        cur = cur.next
                    else:
                        break
                h2 = cur.next
                cur.next = None  # h1 结扎
                cur = h2  # 恢复 cur
                for _ in range(sub_len - 1):
                    if cur and cur.next:
                        cur = cur.next
                    else:
                        break
                suc = None
                if cur:  # 构造新的【cur】
                    suc = cur.next
                    cur.next = None  # h2 结扎
                cur = suc  #
                merged = merge(h1, h2)
                pre.next = merged
                # 构造新的【pre】 (merged的最后一个结点)
                while pre.next:
                    pre = pre.next
            sub_len *= 2
        return dummy.next


print(Solution().sortList(ListNode.fromList([4, 2, 1, 3])))
#
# print(merge(ListNode.fromList([1, 3, 5]), ListNode.fromList([2, 4, 6, 7])))
示例#3
0
from structure import ListNode


class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        # hash = hash * seed + val
        # seed: prime number
        # val: node value
        # hash1 = a0 * seed^(n-1) + a1 * seed^(n-2)
        hash1 = hash2 = 0
        h = 1
        # seed=int(1e9+7)
        seed = 3
        p = head
        while p is not None:
            hash1 = hash1 * seed + p.val
            hash2 = hash2 + h * p.val
            h *= seed
            p = p.next
        return hash1 == hash2


if __name__ == '__main__':
    node = ListNode.fromList([1, 2, 2, 1])
    print(Solution().isPalindrome(node))
示例#4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : qichun tang
# @Contact    : [email protected]
from structure import ListNode


class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:
            return head
        oddHead, evenHead = head, head.next
        odd, even = oddHead, evenHead
        while even and even.next:
            odd.next = even.next
            odd = odd.next  # 别忘了自己要移动
            even.next = odd.next
            even = even.next
        odd.next = evenHead
        return head


if __name__ == '__main__':
    nodes = ListNode.fromList([1, 2, 3, 4, 5])
    solved = Solution().oddEvenList(nodes)
    print(solved)
示例#5
0
    def insertionSortList(self, head: ListNode) -> ListNode:
        if not head: return head
        dummy = ListNode(-inf) # D
        dummy.next = head  # D 4 2 1 3 ∅
        tail = dummy.next  # 4 ∅ // 如果新结点比【有序区】的【尾结点】更大,直接成为新的尾结点
        p = head.next      # 2 1 3 ∅ // 头结点的第一个元素初始为【有序区】,从头结点的【下一个元素】开始遍历
        tail.next = None   # 封闭有序区
        while p:
            aft = p.next # 缓存下一个结点
            if p.val >= tail.val:  # 注意符号
                tail.next = p
                tail = p
            else:
                dp = dummy
                while dp.next:
                    # 首次出现比p大的元素。插入到那个元素前面
                    if dp.next.val > p.val:
                        p.next = dp.next
                        dp.next = p
                        break
                    dp=dp.next
            p = aft
            tail.next = None
        return dummy.next



head = ListNode.fromList([4, 2, 1, 3])
ans = Solution().insertionSortList(head)
print(ans)
示例#6
0
        # 递归终点
        if not head or not head.next:
            return head
        # 中点寻找
        slow = head
        fast = head.next  # 否则爆栈
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        mid = slow.next
        slow.next = None
        # 处理左链表与右链表
        left = self.sortList(head)
        right = self.sortList(mid)
        # 合并两个排序连败哦
        dummy = dp = ListNode(0)
        while left and right:
            if left.val < right.val:
                dp.next = left
                left = left.next
            else:
                dp.next = right
                right = right.next
            dp = dp.next
        dp.next = left if left else right
        return dummy.next


ans = Solution().sortList(ListNode.fromList([1, 5, 3, 4, 0]))
print(ans)
示例#7
0
from structure import ListNode


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None or head.next is None:
            return head
        node = self.reverseList(head.next)
        head.next.next = head
        head.next = None
        return node


print(Solution().reverseList(ListNode.fromList([1, 2, 3, 4, 5])))
示例#8
0
from structure import ListNode


class Solution:
    def swapPairs(self, head: ListNode) -> ListNode:
        if not head: return None
        dummy = ListNode()
        dp = dummy
        p = head
        while p:
            np = p.next
            if np:
                nnp = np.next
                dp.next = np
                dp = dp.next
                dp.next = p
                dp = dp.next
                p = nnp
            else:
                dp.next = p
                dp = dp.next
                break
        if dp:
            dp.next = None
        return dummy.next


print(Solution().swapPairs(ListNode.fromList([1])))
示例#9
0
from structure import ListNode

node = ListNode.fromList([1, 2])


def partition(head: ListNode):
    if not head or not head.next:
        return head
    p1, p2 = head, head.next
    head1, head2 = p1, p2
    # p2 后面至少有1个结点
    while p2 and p2.next:
        p1.next = p2.next
        p1 = p1.next
        p2.next = p1.next
        p2 = p2.next
    p1.next = None
    return head1, head2


print(partition(node))
示例#10
0
from structure import ListNode


class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        p = head
        k = 0
        p2 = None
        while p is not None:
            p = p.next
            k += 1
            if (k - 1) == n:
                p2 = head
            if (k - 1) > n:
                p2 = p2.next
        if p2 is None and k == n:
            head = head.next
        elif p2.next is not None:
            p2.next = p2.next.next
        return head


if __name__ == '__main__':
    node = Solution().removeNthFromEnd(ListNode.fromList([1, 2]), 2)
    print(node)
示例#11
0

class Solution:
    def reorderList(self, head: ListNode) -> None:
        if head is None:
            return None
        p = head
        vec = []
        while p is not None:
            vec.append(p)
            p = p.next
        i = 0
        j = len(vec) - 1
        while i < j:
            vec[i].next = vec[j]
            i += 1
            if i == j:
                break
            vec[j].next = vec[i]
            j -= 1
        vec[i].next = None  # 容易想错


node = ListNode.fromList([1, 2, 3, 4])
Solution().reorderList(node)
print(node)

node = ListNode.fromList([1, 2, 3, 4, 5])
Solution().reorderList(node)
print(node)
示例#12
0
                       right: int) -> ListNode:
        # 以case 1, 2, 3, 4, 5 为例,left = 2, right = 4
        p = head
        L = right - left + 1  # 翻转的部分是 2 3 4, 长度为3
        dummy = ListNode(0)
        dp = dummy
        for _ in range(left - 1):  # 把 翻转部分 2 之前的添加到dummy中(dummy用来返回)
            dp.next = p
            dp = dp.next
            p = p.next
        # 迭代完之后 p=2
        aft = p.next
        pre = None
        l2_tail = p  # 第二段(l2) 的开头,但翻转后会变成末尾
        for _ in range(L):
            aft = p.next
            p.next = pre
            pre = p
            p = aft
        dp.next = pre  # 接上翻转的列表, 即 1 4 3 2
        l2_tail.next = aft  # l2 段 接上剩下没迭代的
        return dummy.next


case1 = ListNode.fromList([3, 5])
ans = Solution().reverseBetween(case1, 1, 2)
print(ans)

# ans = Solution().reverseBetween(ListNode.fromList(list(range(1, 6))), 2, 4)
# print(ans)
示例#13
0

def reverse(a, b):
    pre = None
    p = a
    while p != b:
        aft = p.next
        p.next = pre
        pre = p
        p = aft
    return pre


class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        if head is None:
            return None
        a = head
        b = head
        for _ in range(k):
            if b is None:
                return head
            b = b.next
        n_head = reverse(a, b)
        a.next = self.reverseKGroup(b, k)
        return n_head


ans = Solution().reverseKGroup(ListNode.fromList([1, 2, 3, 4, 5]), 2)
print(ans)
示例#14
0
        if a.val < b.val:
            dp.next = a
            a = a.next
        else:
            dp.next = b
            b = b.next
        dp = dp.next
    dp.next = a if a else b
    return dummy.next


def partition(lists):
    n = len(lists)
    if n == 0:
        return None
    elif n == 1:
        return lists[0]
    else:
        mid = len(lists) // 2
        return merge(partition(lists[:mid]), partition(lists[mid:]))


class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        return partition(lists)


a = ListNode.fromList([1, 3, 5])
b = ListNode.fromList([2, 4])
print(merge(a, b))