Example #1
0

class Solution:
    def __init__(self, head: ListNode):
        """
        @param head The linked list's head.
        Note that the head is guaranteed to be not null, so it contains at least one node.
        """
        self.head = head
        self.c = 0
        while head:
            head = head.next
            self.c += 1

    def getRandom(self) -> int:
        """
        Returns a random node's value.
        """
        if self.head:
            num = random.randint(0, self.c - 1)
            ans = self.head
            while num > 0:
                ans = ans.next
                num -= 1
            return ans.val


a = stringToListNode('[1,2,3]')
obj = Solution(a)
obj.getRandom()
    def findsize(self, head):
        cur = head
        size = 0
        while cur:
            cur = cur.next
            size += 1
        return size

    def sortedListToBST(self, head: ListNode) -> TreeNode:
        size = self.findsize(head)

        def buildtree(l, r):
            nonlocal head
            if l > r:
                return None
            mid = (l + r) // 2
            left = buildtree(l, mid - 1)
            node = TreeNode(head.val)
            head = head.next
            node.left = left
            node.right = buildtree(mid + 1, r)
            return node

        if size == 0:
            return None
        return buildtree(0, size - 1)


a = stringToListNode('[-10,-3,0,5,9]')
Solution().sortedListToBST(a)
Example #3
0

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        H = head
        while head.next:
            lastnode = head
            head = head.next
            p = head.next
            h2 = H
            if int(head.val) > int(lastnode.val):
                lastnode = head
            elif int(head.val) <= int(h2.val):
                head.next = h2
                H = head
            else:
                while int(head.val) > int(h2.val):
                    prev = h2
                    h2 = h2.next
                prev.next = head
                head.next = h2
            lastnode.next = p
            head = lastnode
        return H


from leetcode.trick.listnode.L import stringToListNode

a = stringToListNode('[4,2,1,3]')
Solution().insertionSortList(a)
Example #4
0
        self.val = val
        self.next = next


class Solution:
    def swapNodes(self, head: ListNode, k: int) -> ListNode:
        h = head
        n = 0
        while head:
            n += 1
            head = head.next
        if n < 2:
            return h
        head = h
        nx = 0
        while head:
            nx += 1
            if nx == k:
                a = head
            if nx == n - k + 1:
                b = head
            head = head.next
        a.val, b.val = b.val, a.val
        return h


from leetcode.trick.listnode.L import stringToListNode

a = stringToListNode('[100,90]')
Solution().swapNodes(a, 5)
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return
        prev = None
        while head:
            ne = head.next
            head.next = prev
            prev = head
            head = ne
        return prev


from leetcode.trick.listnode.L import stringToListNode


# 递归写法
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        def rec(node1, node2):
            if not node2:
                return node1
            else:
                ne = node2.next
                node2.next = node1
                return rec(node2, ne)

        return rec(None, head)


a = stringToListNode('[1,2,3,4,5]')
Solution().reverseList(a)
Example #6
0
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。'''
from typing import List
from leetcode.trick.listnode.L import stringToListNode


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:
            return
        H = head
        l = head
        r = head.next
        R = r
        while r and r.next:
            p = r.next
            r.next = r.next.next
            l.next = p
            l, r = l.next, r.next
        l.next = R
        return H


a = stringToListNode('[2]')
Solution().oddEvenList(a)
Example #7
0
        return H.next


# 归并+递归 链表递归 特殊递归
class Solution:
    def sortList(self, head: ListNode) -> ListNode:
        if not head or not head.next: return head  # termination.
        # cut the LinkedList at the mid index.
        slow, fast = head, head.next
        while fast and fast.next:
            fast, slow = fast.next.next, slow.next
        mid, slow.next = slow.next, None  # save and cut.
        # recursive for cutting.
        left, right = self.sortList(head), self.sortList(mid)
        # merge `left` and `right` linked list and return it.
        h = res = ListNode(0)
        while left and right:
            if left.val < right.val: h.next, left = left, left.next
            else: h.next, right = right, right.next
            h = h.next
        h.next = left if left else right
        return res.next


# 作者:jyd
# 链接:https://leetcode-cn.com/problems/sort-list/solution/sort-list-gui-bing-pai-xu-lian-biao-by-jyd/
# 来源:力扣(LeetCode)
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
b = stringToListNode('[3,4,1,9,18,27,36]')
Solution().sortList(b)