[LeetCode] Rotate List

来源:互联网 发布:手机金属探测仪软件 编辑:程序博客网 时间:2024/06/15 19:27
ListNode *rotateRight(ListNode *head, int k) {if(head == NULL){return head;}else if(head->next == NULL){return head;}while(k > 0){ListNode* ptr1 = head;ListNode* ptr2 = ptr1->next;while(ptr2->next != NULL){ptr1 = ptr1->next;ptr2 = ptr2->next;}ptr1->next = NULL;ptr2->next = head;head = ptr2;k--;}return head;}


先实现链表循环右移1位,再将其循环k次,即完成了右移k位

0 0
原创粉丝点击