LeetCode:Reverse Nodes in k-Group

1、题目名称

Reverse Nodes in k-Group(分组翻转链表)

2、题目地址

https://leetcode.com/problems/reverse-nodes-in-k-group

3、题目内容

英文:

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.

中文:

给出一个链表,以k个元素为一组,对各组内元素进行翻转。忽略最后不够k的元素的部分。可以只对节点中的值进行交换,空间复杂度须控制在O(1)。

举例:

给出链表:1->2->3->4->5

当 k = 2 时,翻转后的链表为:2->1->4->3->5

当 k = 3 时,翻转后的链表为:3->2->1->4->5

4、解题方法

对这个问题,在给出链表的首节点nodeCurr和每组节点数k后,可以分解成三个子问题:

  1. 判断链表剩余部分是否还需要翻转(剩余部分不够k个元素就不翻转)
  2. 翻转链表中包括首节点在内的前k个元素
  3. 定位到下一组的首节点

循环执行这三个函数,直到执行第一步时判定为不需要翻转为止

一个实现此算法的Java代码如下:

/**
 * 功能说明:LeetCode 25 - Reverse Nodes in k-Group
 * 开发人员:Tsybius2014
 * 开发时间:2015年8月10日
 */
public class Solution {
    
    /**
     * 以k个结点一组来翻转链表
     * @param head
     * @param k
     * @return
     */
    public ListNode reverseKGroup(ListNode head, int k) {
        
        ListNode nodeCurr = head;
        while (needToReverse(nodeCurr, k)) {
            reverseOneKGroup(nodeCurr, k);
            nodeCurr = nextKGroup(nodeCurr, k);
        }
        
        return head;
    }
    
    /**
     * 判断剩余长度是否有必要进行翻转
     * @param listNode
     * @param k
     * @return
     */
    public boolean needToReverse(ListNode node, int k) {
        if (node == null) {
            return false;
        }
        while (--k != 0) {
            node = node.next;
            if (node == null) {
                return false;
            }
        }
        return true;
    }

    /**
     * 翻转一组长度为k的链
     * @param node
     * @param k
     */
    public void reverseOneKGroup(ListNode node, int k) {
        ListNode nodeToReverse; //要互换值的对称位置结点
        int distance = k - 1; //当前结点距离要翻转的对称位置结点的长度
        int temp;
        for (int i = 0; i < k / 2; i++) {
            if (i > 0) {
                node = node.next;
                distance -= 2;
            }
            if (node == null) {
                return;
            }
            nodeToReverse = node;
            for (int j = 0; j < distance; j++) {
                nodeToReverse = nodeToReverse.next;
                if (nodeToReverse == null) {
                    return;
                }
            }
            temp = node.val;
            node.val = nodeToReverse.val;
            nodeToReverse.val = temp;
        }
    }
    
    /**
     * 下一组结点
     * @param listNode
     * @return
     */
    public ListNode nextKGroup(ListNode node, int k) {
        if (node == null) {
            return null;
        }
        while (k-- != 0) {
            node = node.next;
            if (node == null) {
                return null;
            }
        }
        return node;
    }
}

END

时间: 2024-08-06 10:51:07

LeetCode: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] 025. Reverse Nodes in k-Group (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 025. Reverse Nodes in k-Group (Hard) 链接: 题目:https://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表每

[LeetCode 题解]: Reverse Nodes in K-Groups

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

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

每日算法之二十三: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

LeetCode 025 Reverse Nodes in k-Group

题目描述: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

[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 Add to List 划分list

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