标签:solution span def col eve ext delete 难度 nod
25. K 个一组翻转链表
/** * 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) { ListNode *current, *after, *head_res, *first, *temp, *pre; current = head;//current = 1 if(k==1||head==NULL||head->next==NULL) return head; head_res = new ListNode; head_res->next = head; pre = head_res; int size = 0; current = head; while(current!=NULL) { current = current->next; size++; } int i, j; for(j = 1; j <= size/k; j++) { current = pre->next; //current = 3 after = current->next; //after = 4 first = current; //first = 3 for(i = 1; i <= k-1; i++) { temp = after->next;//temp = 5 after->next = current;//4->3 current = after;//current = 4 after = temp;//after = 5 } pre->next = current;//1->3 first->next = after;//3->5 pre = first;//pre = 3 } return head_res->next; delete(head_res); } };
标签:solution span def col eve ext delete 难度 nod
原文地址:https://www.cnblogs.com/jessica216/p/13381703.html