LeetCode (25) Reverse Nodes in k-Group (链表)

题目描述

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

本题考查的是链表的操作。题目要求,输入一个链表和一个数字k,数字k表示链表中每k个数字进行一次逆转。题目中要求通过改变结点位置实现要求,而不能通过改变结点的value达到目的(改变value显然是一种作弊的行为 o(╯□╰)o)。

解决思路

这道题目还是比较简单的,示意图如下:

将待逆转的结点保存到Stack中,并且保存逆转结点的前一个指针Priv和下一个指针Next,从Priv指针开始依次链接出栈的结点,并将最后出栈的结点的下一个结点设置为Next即可。

需要注意的地方:

  • 第一次逆转的时候Priv = NULL,此时需要设置栈顶的指针为链表的head指针。
  • 另外对于链表最后不满k个元素的时候,入栈结点个数小于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) {
        if (!head || k <= 1)
            return head;

        ListNode* h = head;
        ListNode* priv = NULL;
        stack<ListNode*> stackNodes;

        ListNode* l1 = head;
        ListNode* l2 = head;
        int count = 0;
        while (l2)
        {
            if (count != k-1)
            {
                count++;
                stackNodes.push(l2);
                l2 = l2->next;
            }
            else
            {
                l1 = l2;
                l2 = l2->next;
                count = 0;
                if (priv)
                    priv->next = l1;
                else
                    h = l1;

                while (!stackNodes.empty())
                {
                    l1->next = stackNodes.top();
                    l1 = l1->next;
                    stackNodes.pop();
                }
                priv = l1;
                priv->next = l2;
            }
        }
        return h;
    }
};
时间: 2024-07-31 03:30:02

LeetCode (25) Reverse Nodes in k-Group (链表)的相关文章

(Java) LeetCode 25. 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. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode] 25. 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. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

Reverse Nodes in k-Group 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 valu

[LeetCode]25. 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. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode 25 Reverse Nodes in k-Group Add to List 划分list

题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分为k个组,并且每个组的元素逆置 链表操作 :递归算法 每次寻找到该组的尾部,然后进行逆置操作,返回头部,这样每次递归操作之后能够进行下一次的逆置操作. 链表操作画图比较形象!!!! 对于递归算法:找到共同点,找到程序退出点,注意特殊情况 本题中,共同点为每组为k个节点,并且每组进行的操作均为逆置操作.

LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)

Problem: 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,

LeetCode 25 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

[LeetCode] 25. Reverse Nodes in k-Group ☆☆☆

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

leetcode 25: Reverse Nodes in k-Group

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 nod