[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 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个时,不进行倒置操作。

思路:

可以利用插入法进行链表倒置,采用递归方法。

(1)链表倒置,可以参考文章:

(2)将链表分成k元素小组。

  设置一个指针pre,指向插入点。

  当插入的元素满足k个后,进行递归。

  例如:链表为 1->2->3->4->5  k=2

  可以将小组分为 1->2 3->4 5

  for i range from 1 to k

    tmp <- head

    #将tmp插入到pre之后

    head = head->next

(3)递归

  上述步骤完成时, head->val = 3,进入到下一次处理

(4)终止条件, 剩余的组中元素不足k个,将该组直接返回,不做倒置。

class Solution {
public:
    int getListLen(ListNode *head){
        int len=0;
        ListNode *tmp=head;
        while(tmp){len++;tmp=tmp->next;}
        return len;
    }
    ListNode *reverseKGroup(ListNode *head, int k) {
       if(k<=1|| !head || !head->next) return head;
        int len=getListLen(head);
        if(len<k) return head;

        ListNode *pre=new ListNode(0);
        ListNode *change=head;

        for(int j=1;j<=k;j++){
                ListNode *tmp = change;
                change = change->next;
                tmp->next = pre->next;
                pre->next=tmp;
        }
        head->next=reverseKGroup(change,k);
        return pre->next;
    }
};

-------------------------------------------------


作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3896010.html 

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解]: Reverse Nodes in K-Groups,布布扣,bubuko.com

时间: 2024-10-24 22:26:35

[LeetCode 题解]: Reverse Nodes in K-Groups的相关文章

(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-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 multipl

【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 =

[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][JavaScript]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 valu

【leetcode】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 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个节点,并且每组进行的操作均为逆置操作.