本文目录
题目描述
给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
示例 1:
复制代码
- 输入:head = [1,2,3,4,5], k = 2
- 输出:[2,1,4,3,5]
示例 2:
复制代码
- 输入:head = [1,2,3,4,5], k = 3
- 输出:[3,2,1,4,5]
个人C++解答
复制代码
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- ListNode* reverseKGroup(ListNode* head, int k) {
- ListNode* result = new ListNode();
- ListNode* p = head;
- ListNode* q = result;
- int ListLen = 0, count = 0;
- while (p) {
- ListLen++;
- p = p->next;
- }
- p = head;
- stack<int> stk;
- while (p) {
- int time = k;
- while (time--) {
- stk.push(p->val);
- p = p->next;
- }
- time = k;
- while (time--) {
- q->next = new ListNode(stk.top());
- q = q->next;
- stk.pop();
- count++;
- }
- if (ListLen - count < k)
- break;
- }
- q->next = p;
- return result->next;
- }
- };
官方题解
复制代码
- class Solution {
- public:
- // 翻转一个子链表,并且返回新的头与尾
- pair<ListNode*, ListNode*> myReverse(ListNode* head, ListNode* tail) {
- ListNode* prev = tail->next;
- ListNode* p = head;
- while (prev != tail) {
- ListNode* nex = p->next;
- p->next = prev;
- prev = p;
- p = nex;
- }
- return {tail, head};
- }
- ListNode* reverseKGroup(ListNode* head, int k) {
- ListNode* hair = new ListNode(0);
- hair->next = head;
- ListNode* pre = hair;
- while (head) {
- ListNode* tail = pre;
- // 查看剩余部分长度是否大于等于 k
- for (int i = 0; i < k; ++i) {
- tail = tail->next;
- if (!tail) {
- return hair->next;
- }
- }
- ListNode* nex = tail->next;
- // 这里是 C++17 的写法,也可以写成
- // pair<ListNode*, ListNode*> result = myReverse(head, tail);
- // head = result.first;
- // tail = result.second;
- tie(head, tail) = myReverse(head, tail);
- // 把子链表重新接回原链表
- pre->next = head;
- tail->next = nex;
- pre = tail;
- head = tail->next;
- }
- return hair->next;
- }
- };
- 作者:力扣官方题解
- 链接:https://leetcode.cn/problems/reverse-nodes-in-k-group/solutions/248591/k-ge-yi-zu-fan-zhuan-lian-biao-by-leetcode-solutio/
- 来源:力扣(LeetCode)
- 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。