Notes: | |
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 | |
Solution: ... | |
*/ | |
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ |
class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
if (k <= 1) return head;
int reverseTimes = GetLength(head) / k;
ListNode dummy(0); dummy.next = head;
ListNode *ins = &dummy, *cur = ins->next;
while (reverseTimes--) //表示反转的几处位置
{
for (int i = 0; i < k - 1; ++i) {//表示要反转接点的个数
ListNode *move = cur->next;
cur->next = move->next;
move->next = ins->next;
ins->next = move;
}
ins = cur;
cur = ins->next;
}
return dummy.next;
}
int GetLength(ListNode *head) {
int length = 0;
while (head) {
head = head->next;
length++;
}
return length;
}
};