Skip to content

Commit c06b614

Browse files
authored
Merge pull request #1531 from ivanpenaloza/january29
adding algo
2 parents d4b7dd4 + 3092f3a commit c06b614

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
# Definition for singly-linked list.
5+
class ListNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
class Solution:
11+
def rotateRight(self, head: ListNode, k: int) -> ListNode:
12+
# Edge cases
13+
if not head or not head.next or k == 0:
14+
return head
15+
16+
# Find length and last node
17+
length = 1
18+
current = head
19+
while current.next:
20+
current = current.next
21+
length += 1
22+
23+
# Calculate effective rotations
24+
k = k % length
25+
if k == 0:
26+
return head
27+
28+
# Find new tail (at position length - k - 1)
29+
new_tail = head
30+
for _ in range(length - k - 1):
31+
new_tail = new_tail.next
32+
33+
# New head is next to new tail
34+
new_head = new_tail.next
35+
36+
# Break the link
37+
new_tail.next = None
38+
39+
# Connect old tail to old head
40+
current.next = head
41+
42+
return new_head
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ListNode } from './ListNode';
2+
3+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
4+
// Edge cases
5+
if (!head || !head.next || k === 0) {
6+
return head;
7+
}
8+
9+
// Find length and last node
10+
let length = 1;
11+
let current = head;
12+
while (current.next) {
13+
current = current.next;
14+
length++;
15+
}
16+
17+
// Calculate effective rotations
18+
k = k % length;
19+
if (k === 0) {
20+
return head;
21+
}
22+
23+
// Find new tail (at position length - k - 1)
24+
let newTail = head;
25+
for (let i = 0; i < length - k - 1; i++) {
26+
newTail = newTail.next!;
27+
}
28+
29+
// New head is next to new tail
30+
const newHead = newTail.next;
31+
32+
// Break the link
33+
newTail.next = null;
34+
35+
// Connect old tail to old head
36+
current.next = head;
37+
38+
return newHead;
39+
}
40+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_63_rotate_linked_list import Solution, ListNode
4+
5+
class RotateLinkListTestCase(unittest.TestCase):
6+
7+
def create_linked_list(self, values):
8+
"""
9+
Helper function to create a linked list from a list of values.
10+
11+
:param values: List of node values
12+
:return: Head of the linked list
13+
"""
14+
if not values:
15+
return None
16+
17+
head = ListNode(values[0])
18+
current = head
19+
20+
for val in values[1:]:
21+
current.next = ListNode(val)
22+
current = current.next
23+
24+
return head
25+
26+
def linked_list_to_list(self, head):
27+
"""
28+
Helper function to convert linked list to Python list.
29+
30+
:param head: Head of the linked list
31+
:return: List of values
32+
"""
33+
result = []
34+
current = head
35+
36+
while current:
37+
result.append(current.val)
38+
current = current.next
39+
40+
return result
41+
42+
def test_first_pattern(self):
43+
# Example 1: Input: head = [1,2,3,4,5], k = 2
44+
# Output: [4,5,1,2,3]
45+
solution = Solution()
46+
head = self.create_linked_list([1, 2, 3, 4, 5])
47+
output = solution.rotateRight(head, 2)
48+
result = self.linked_list_to_list(output)
49+
target = [4, 5, 1, 2, 3]
50+
self.assertEqual(result, target)
51+
52+
def test_second_pattern(self):
53+
# Example 2: Input: head = [0,1,2], k = 4
54+
# Output: [2,0,1]
55+
solution = Solution()
56+
head = self.create_linked_list([0, 1, 2])
57+
output = solution.rotateRight(head, 4)
58+
result = self.linked_list_to_list(output)
59+
target = [2, 0, 1]
60+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)