Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
反转单链表的基础题。
1:不用额外空间
2:不改动结点值,很容易写错
3:建议把Reverse()这个函数背下来。
// // ReverseNodesInk-Group.c // Algorithms // // Created by TTc on 15/6/19. // Copyright (c) 2015年 TTc. All rights reserved. // /** * Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nodes itself may be changed. Only constant memory is allowed. For example, Given this linked list: 1->2->3->4->5 For k = 2, you should return: 2->1->4->3->5 For k = 3, you should return: 3->2->1->4->5 反转链表的基础题。 思路很清楚,但是在不用额外空间和不改动结点值这两个条件下,很容易写错。建议把Reverse()这个函数背下来。 */ #include "ReverseNodesInk-Group.h" #include <stdlib.h> #include <string.h> #include "List.h" /********************************************************************/ // LeetCode 答案 /********************************************************************/ struct ListNode { int val; struct ListNode *next; }; //双指针大法 /*Since only constant memory is allowed, time cost is O(k) */ static struct ListNode* Reverse(struct ListNode *begin, struct ListNode *end){ struct ListNode* prev = begin->next; struct ListNode* curr = prev->next; while(curr != end){ prev->next = curr->next; curr->next = begin->next; begin->next = curr; curr = prev->next; } return prev; } struct ListNode* reverseKGroup(struct ListNode* head, int k) { if(head == NULL || head->next == NULL || k == 1) return head; struct ListNode *dummyHead = (struct ListNode *)malloc(sizeof(*dummyHead)); dummyHead->next = head; struct ListNode *prev = head; struct ListNode *curr = dummyHead; int count = 0; while(prev != NULL){ count++; if(count == k){ curr = Reverse(curr, prev->next); prev = curr->next; count = 0; } else prev = prev->next; } return dummyHead->next; } /********************************************************************/ /********************************************************************/ /********************************************************************/ // List.c 是范性类 单链表 /********************************************************************/ //反转单链表中 begin 到 end 节点 static ListElmt* tt_Reverse(ListElmt *begin, ListElmt *end){ ListElmt* prev = begin->next; ListElmt* curr = prev->next; while(curr != end){ prev->next = curr->next; curr->next = begin->next; begin->next = curr; curr = prev->next; } return prev; } //反转 长度为k的 每组 节点 ListElmt* tt_reverseKGroup(ListElmt * head, int k) { if(head == NULL || head->next == NULL || k == 1) return head; ListElmt *dummyHead = (ListElmt *)malloc(sizeof(*dummyHead)); dummyHead->next = head; ListElmt *prev = head; ListElmt *curr = dummyHead; int count = 0; while(prev != NULL){ count++; if(count == k){ curr = tt_Reverse(curr, prev->next); prev = curr->next; count = 0; } else prev = prev->next; } return dummyHead->next; } void test_tt_reverseKGroup(){ List l1; list_init(&l1, free); int *data ; int array[15] = {100,200,300,4000,5000,600,700,800,900,100000}; for (int i = 0; i< 10; i++) { if ((data = (int *)malloc(sizeof(int))) == NULL) return ; *data = array[i]; if (list_ins_next(&l1, NULL, data) != 0) //逐个插入元素 return; } print_list(&l1); ListElmt *result = tt_reverseKGroup(list_head(&l1),3); printf("result->val===%d\n",*(int *)result->data); print_listNode(result); } /********************************************************************/ /********************************************************************/