[LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

简介:

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.

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

LeetCode上的原题,请参见我之前的博客Reverse Nodes in k-Group 每k个一组翻转链表

解法一:

class Solution {
public:
    /**
     * @param head a ListNode
     * @param k an integer
     * @return a ListNode
     */
    ListNode *reverseKGroup(ListNode *head, int k) {
        ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = pre;
        dummy->next = head;
        while (cur->next) {
            int d = k;
            while (cur->next && d-- > 0) {
                cur = cur->next;
            }
            if (d > 0) return dummy->next;
            ListNode *t = cur->next;
            cur->next = NULL;
            pre->next = reverse(pre->next);
            for (int i = 0; i < k; ++i) pre = pre->next;
            pre->next = t;
            cur = pre;
        }
        return dummy->next;
    }
    ListNode* reverse(ListNode *head) {
        ListNode *dummy = new ListNode(-1), *cur = head;
        dummy->next = head;
        while (cur && cur->next) {
            ListNode *t = cur->next;
            cur->next = t->next;
            t->next = dummy->next;
            dummy->next = t;
        }
        return dummy->next;
    }
};

解法二:

class Solution {
public:
    /**
     * @param head a ListNode
     * @param k an integer
     * @return a ListNode
     */
    ListNode *reverseKGroup(ListNode *head, int k) {
        ListNode *dummy = new ListNode(-1), *pre = dummy, *cur = pre;
        dummy->next = head;
        int num = 0;
        while (cur = cur->next) ++num;
        while (num >= k) {
            cur = pre->next;
            for (int i = 1; i < k; ++i) {
                ListNode *t = cur->next;
                cur->next = t->next;
                t->next = pre->next;
                pre->next = t;
            }
            pre = cur;
            num -= k;
        }
        return dummy->next;
    }
};

解法三:

class Solution {
public:
    /**
     * @param head a ListNode
     * @param k an integer
     * @return a ListNode
     */
    ListNode *reverseKGroup(ListNode *head, int k) {
        ListNode *cur = head;
        for (int i = 0; i < k; ++i) {
            if (!cur) return head;
            cur = cur->next;
        }
        ListNode *new_head = reverse(head, cur);
        head->next = reverseKGroup(cur, k);
        return new_head;
    }
    ListNode* reverse(ListNode* head, ListNode* tail) {
        ListNode *pre = tail;
        while (head != tail) {
            ListNode *t = head->next;
            head->next = pre;
            pre = head;
            head = t;
        }
        return pre;
    }
};

本文转自博客园Grandyang的博客,原文链接:每k个一组翻转链表[LintCode] Reverse Nodes in k-Group ,如需转载请自行联系原博主。

相关文章
【 腾讯精选练习 50 题】01—翻转链表
【 腾讯精选练习 50 题】01—翻转链表
|
4月前
|
算法 Java
「LeetCode」25. K 个一组翻转链表
「LeetCode」25. K 个一组翻转链表
27 0
|
3月前
|
算法 Java Go
Golang每日一练(leetDay0098) 生命、Nim、猜数字游戏
Golang每日一练(leetDay0098) 生命、Nim、猜数字游戏
28 0
Golang每日一练(leetDay0098) 生命、Nim、猜数字游戏
|
3月前
|
Java Go 人工智能
Java每日一练(20230502) BST公共祖先、随机分组、K个一组翻转链表
Java每日一练(20230502) BST公共祖先、随机分组、K个一组翻转链表
25 0
Java每日一练(20230502) BST公共祖先、随机分组、K个一组翻转链表
|
3月前
|
Python Java Go
Python每日一练(20230430) 移除元素、删除排序链表中的重复元素、搜索旋转排序数组II
Python每日一练(20230430) 移除元素、删除排序链表中的重复元素、搜索旋转排序数组II
46 0
Python每日一练(20230430) 移除元素、删除排序链表中的重复元素、搜索旋转排序数组II
|
3月前
leetcode-25:K 个一组翻转链表
leetcode-25:K 个一组翻转链表
16 0
LeetCode刷题Day04——链表(设计单/双链表、移除、翻转、交换链表节点)
迭代法:首先创建一个临时的节点p用于遍历链表,其开始可以指向头节点,也可以让其next节点指向头节点(如果p指向头节点则while循环的判断条件是p!=null,反之则是p.next!=null),随后p不断地向后移动,在这个过程中进行要求的操作。如果结果要返回头指针,可以实现创建一个节点让其next指向头指针。 如果是要删除元素,那么需要拥有前一个节点的指针,让其指向要删除的元素的下一个元素,所以此时则不能让p指向头节点,而应该是让next去指向,即判断的是下一个元素的值,这样才能够实现删除。 如果是要翻转链表,那么需要不断改变指针的方向,即让next等于之前的元素,所以需要一个变量prev
|
1月前
|
算法
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
LeetCode刷题---19. 删除链表的倒数第 N 个结点(双指针-快慢指针)
|
1月前
|
存储
LeetCode刷题---817. 链表组件(哈希表)
LeetCode刷题---817. 链表组件(哈希表)
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解