bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 61. Rotate List

61. Rotate List

Leetcode Solutions

Rotating a Linked List

  1. Check if the head is null or k is 0, return head as is.
  2. Initialize a variable to keep track of the length of the list and set it to 1 (since we start with the head).
  3. Traverse the list to find the tail and calculate the length.
  4. Connect the tail to the head to form a ring.
  5. Find the new tail, which is at position (n - k % n - 1) from the head.
  6. Find the new head, which is the next node of the new tail.
  7. Break the ring by setting new_tail.next to None.
  8. Return the new head as the start of the rotated list.
UML Thumbnail

Brute Force Rotation of a Linked List

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...