示例#1
0
            return head
        elif not fast and counter < k:

            k = k % counter
            counter = 0
            fast = head
            if k == 0:
                return head
            while counter < k and fast:
                fast = fast.next
                counter += 1

        while fast.next:
            fast = fast.next
            slow = slow.next
        fast.next = head
        newHead = slow.next
        slow.next = None
        return newHead


l1 = ListNode()
l1.create_linklist([1, 2, 3, 4, 5])
k = 11
l1.printLL()
s = Solution()

for i in range(20):
    r = s.rotateRight(l1, i)
    r.printLL()
示例#2
0
#https://leetcode.com/problems/middle-of-the-linked-list/
from linkedList import ListNode


class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        if not head:
            return None
        slow, fast = head, head
        while fast:
            fast = fast.next
            if not fast:
                return slow
            fast = fast.next
            slow = slow.next

            if not fast:
                return slow

        return slow


head = ListNode(1)
head.append([2, 3])
head.printLL()

s = Solution()

r = s.middleNode(head)
print(r.val)