Python for singly-linked list


  • 0
    D

    Python for singly-linked list.

    class ListNode(object):
    def init(self, x):
    self.val = x
    self.next = None

    class Solution(object):
    def rotateRight(self, head, k):
    """
    :type head: ListNode
    :type k: int
    :rtype: ListNode
    """
    if head == None:return
    sign = head
    m = head
    i = 1
    while m.next:
    m = m.next
    i += 1
    m.next = head
    k = i - k%(i)
    i = 0
    while i < k:
    m = m.next
    i += 1
    head = m.next
    m.next = None
    return head


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.