Mark: beat 100% python submissions, first time ever for me!


  • 0
    E

    Just finished the following code and the OJ said it beat 100%! May be improved though.

        def rotateRight(self, head, k):
            """
            :type head: ListNode
            :type k: int
            :rtype: ListNode
            """
            if not head:
                return head
            dummy = ListNode(0)
            dummy.next = head
            pre = dummy
            p1 = p2 = 0
            while head.next:
                p2 += 1
                if p2-p1 == k:
                    pre = pre.next
                    p1 += 1
                head = head.next
            if not p1:
                k = k%(p2+1)
                head = dummy.next
                p1 = p2 = 0
                while head.next:
                    p2 += 1
                    if p2-p1 == k:
                        pre = pre.next
                        p1 += 1
                    head = head.next
            if p1:
                head.next = dummy.next
                dummy.next = pre.next
                pre.next = None
            return dummy.next
    

Log in to reply
 

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