leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit a4decacf501f8189aede3358a641fab291dfd7ce
parent 48092c680db162869b6a39e8e372bbb0f1bfb399
Author: Jack Sangdahl <jack@pngu.org>
Date:   Mon,  4 May 2026 21:31:10 -0600

61. rotate list

Diffstat:
Mleetcode.c | 5++++-
Mleetcode.h | 2+-
Arotate_list.c | 37+++++++++++++++++++++++++++++++++++++
Arotate_list.py | 36++++++++++++++++++++++++++++++++++++
4 files changed, 78 insertions(+), 2 deletions(-)

diff --git a/leetcode.c b/leetcode.c @@ -39,11 +39,14 @@ struct ListNode *listnode_build(int *vals, int size) { return head; } -void listnode_print(struct ListNode *head) { +void listnode_print(struct ListNode *head, int *expected) { struct ListNode *curr = head; + int n = 0; while (curr != NULL) { printf("%d ", curr->val); + assert(curr->val == expected[n]); curr = curr->next; + n++; } printf("\n"); } diff --git a/leetcode.h b/leetcode.h @@ -41,7 +41,7 @@ void two_d_arr_free(struct two_d_arr *obj); struct ListNode *listnode_create(int val); struct ListNode *listnode_build(int *vals, int size); -void listnode_print(struct ListNode *head); +void listnode_print(struct ListNode *head, int *expected); void listnode_free(struct ListNode *head); struct TreeNode *treenode_create(int val); diff --git a/rotate_list.c b/rotate_list.c @@ -0,0 +1,37 @@ +// 61. Rotate List +#include "leetcode.h" + +/* + * given the 'head' of a linked list, rotate the list to the rigt by 'k' places. + */ + +struct ListNode *rotateRight(struct ListNode *head, int k) { + if (!head) + return NULL; + int n = 1; + struct ListNode *tail = head; + while (tail->next) { + n++; + tail = tail->next; + } + tail->next = head; + int m = n - k % n; + for (int i = 0; i < m; i++) + tail = tail->next; + head = tail->next; + tail->next = NULL; + return head; +} + +int main() { + int h1i[] = {1, 2, 3, 4, 5}, r1[] = {4, 5, 1, 2, 3}; + int h2i[] = {0, 1, 2}, r2[] = {2, 0, 1}; + struct ListNode *h1 = listnode_build(h1i, ARRAY_SIZE(h1i)); + struct ListNode *h2 = listnode_build(h2i, ARRAY_SIZE(h2i)); + struct ListNode *rr1 = rotateRight(h1, 2); + struct ListNode *rr2 = rotateRight(h2, 4); + listnode_print(rr1, r1); + listnode_print(rr2, r2); + listnode_free(h1); + listnode_free(h2); +} diff --git a/rotate_list.py b/rotate_list.py @@ -0,0 +1,36 @@ +# 61. Rotate List +from typing import Optional + +from leetcode import ListNode, listnode_build + +""" +given the 'head' of a linked list, rotate the list to the rigt by 'k' places. +""" + + +class Solution: + def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: + if not head or not head.next: + return head + curr = head + len = 1 + while curr.next: + curr = curr.next + len += 1 + curr.next = head + k = k % len + print(k) + print(len) + for i in range(len - k): + curr = curr.next + head = curr.next + curr.next = None + return head + + +if __name__ == "__main__": + obj = Solution() + h1 = listnode_build(vals=[1, 2, 3, 4, 5]) + h2 = listnode_build(vals=[0, 1, 2]) + print(obj.rotateRight(h1, k=2)) + print(obj.rotateRight(h1, k=4))