/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* rotateRight(struct ListNode* head, int k){
if (head == NULL || head->next == NULL) return head;
int len = 0;
for(struct ListNode *p = head; p; p = p->next) len++;
int n = k % len;
if (n == 0) return head;
struct ListNode *p = head;
for (int i = 0; i < len - n - 1; i++) p = p->next;
struct ListNode *q = p->next;
p->next = NULL;
p = q;
while (p->next) p = p->next;
p->next = head;
return q;
}
leetcode-61:旋转链表
最新推荐文章于 2024-11-16 14:46:58 发布