Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
my solution

Mohammed Dh Abbas

Jul 28, 2024

#class Node: # def __init__(self, value, next=None): # self.val = value # self.next = next class Solution: def rotate(self, head, rotations): def get_moves(): length = 0 node = head while node: node = node.next length += 1 remain = length - (rotations % length) return remain # get the number of moves moves = get_moves() # move the nodes based on the number of moves count = 1 node = head while count < moves: count += 1 node = node.next # cut the linkedlist to 2 parts next_part = node.next node.next = None # move to the end of the second part node = next_part while node and node.next: node = node.next # link the 2 parts again if node: node.next = head else: next_part = head # there is no rotation return next_part

0

0

Comments
Comments

On this page