diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_63_rotate_linked_list.py b/src/my_project/interviews/top_150_questions_round_22/ex_63_rotate_linked_list.py new file mode 100644 index 00000000..7a756484 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_63_rotate_linked_list.py @@ -0,0 +1,42 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def rotateRight(self, head: ListNode, k: int) -> ListNode: + # Edge cases + if not head or not head.next or k == 0: + return head + + # Find length and last node + length = 1 + current = head + while current.next: + current = current.next + length += 1 + + # Calculate effective rotations + k = k % length + if k == 0: + return head + + # Find new tail (at position length - k - 1) + new_tail = head + for _ in range(length - k - 1): + new_tail = new_tail.next + + # New head is next to new tail + new_head = new_tail.next + + # Break the link + new_tail.next = None + + # Connect old tail to old head + current.next = head + + return new_head \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_63_rotate_linked_list.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_63_rotate_linked_list.ts new file mode 100644 index 00000000..85747f3f --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_63_rotate_linked_list.ts @@ -0,0 +1,40 @@ +import { ListNode } from './ListNode'; + +function rotateRight(head: ListNode | null, k: number): ListNode | null { + // Edge cases + if (!head || !head.next || k === 0) { + return head; + } + + // Find length and last node + let length = 1; + let current = head; + while (current.next) { + current = current.next; + length++; + } + + // Calculate effective rotations + k = k % length; + if (k === 0) { + return head; + } + + // Find new tail (at position length - k - 1) + let newTail = head; + for (let i = 0; i < length - k - 1; i++) { + newTail = newTail.next!; + } + + // New head is next to new tail + const newHead = newTail.next; + + // Break the link + newTail.next = null; + + // Connect old tail to old head + current.next = head; + + return newHead; +} + diff --git a/tests/test_150_questions_round_22/test_63_rotate_linked_list_round_22.py b/tests/test_150_questions_round_22/test_63_rotate_linked_list_round_22.py new file mode 100644 index 00000000..9d5f23cc --- /dev/null +++ b/tests/test_150_questions_round_22/test_63_rotate_linked_list_round_22.py @@ -0,0 +1,60 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_63_rotate_linked_list import Solution, ListNode + +class RotateLinkListTestCase(unittest.TestCase): + + def create_linked_list(self, values): + """ + Helper function to create a linked list from a list of values. + + :param values: List of node values + :return: Head of the linked list + """ + if not values: + return None + + head = ListNode(values[0]) + current = head + + for val in values[1:]: + current.next = ListNode(val) + current = current.next + + return head + + def linked_list_to_list(self, head): + """ + Helper function to convert linked list to Python list. + + :param head: Head of the linked list + :return: List of values + """ + result = [] + current = head + + while current: + result.append(current.val) + current = current.next + + return result + + def test_first_pattern(self): + # Example 1: Input: head = [1,2,3,4,5], k = 2 + # Output: [4,5,1,2,3] + solution = Solution() + head = self.create_linked_list([1, 2, 3, 4, 5]) + output = solution.rotateRight(head, 2) + result = self.linked_list_to_list(output) + target = [4, 5, 1, 2, 3] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Example 2: Input: head = [0,1,2], k = 4 + # Output: [2,0,1] + solution = Solution() + head = self.create_linked_list([0, 1, 2]) + output = solution.rotateRight(head, 4) + result = self.linked_list_to_list(output) + target = [2, 0, 1] + self.assertEqual(result, target) \ No newline at end of file