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

其中,创建了一个dump充当头结点,使得操作统一。使用pre来记录要逆转的序列的前面一个结点,而q记录要逆转序列之后的一个结点,每次逆转返回逆转之后的最后一个元素,也就是下一次逆转的前驱。

 

C++代码实现:

#include<iostream>
#include<new>
#include<vector>
#include<cmath>
using namespace std;

//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==NULL||head->next==NULL)
            return head;
        int len=0;
        ListNode *p=head;
        ListNode *q=NULL;
        ListNode *pre=NULL;
        while(p)
        {
            len++;
            p=p->next;
        }
        ListNode *dump=new ListNode(0);
        dump->next=head;
        pre=dump;
        p=head;
        int count=0;
        while(p)
        {
            count++;
            q=p->next;
            while(count==k)
            {
                pre=reverse(pre,q);
                count=0;
            }
            p=q;
        }
        return dump->next;
    }
    ListNode *reverse(ListNode *pre,ListNode *q)
    {
        if(pre->next==NULL)
            return NULL;
        if(pre->next->next==NULL)
            return pre->next;
        ListNode *p=pre->next;
        ListNode *pp=NULL;
        ListNode *temp=NULL;
        pp=p->next;
        p->next=NULL;
        while(pp!=q)
        {
            temp=pp;
            pp=pp->next;
            temp->next=NULL;
            temp->next=pre->next;
            pre->next=temp;

        }
        p->next=q;
        pre=p;
        return pre;
    }
    void createList(ListNode *&head)
    {
        ListNode *p=NULL;
        int i=0;
        int arr[10]= {9,8,7,6,5,4,3,2,1,0};
        for(i=0; i<10; i++)
        {
            if(head==NULL)
            {
                head=new ListNode(arr[i]);
                if(head==NULL)
                    return;
            }
            else
            {
                p=new ListNode(arr[i]);
                p->next=head;
                head=p;
            }
        }
    }
};
int main()
{
    Solution s;
    ListNode *L=NULL;
    s.createList(L);
    ListNode *head=L;
    while(head)
    {
        cout<<head->val<<" ";
        head=head->next;
    }
    cout<<endl;
    L=s.reverseKGroup(L,3);
    head=L;
    while(head)
    {
        cout<<head->val<<" ";
        head=head->next;
    }
    cout<<endl;
}

运行结果:

时间: 2024-10-28 22:05:48

Reverse Nodes in k-Group的相关文章

[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, onl

LeetCode 25 Reverse Nodes in k-Group(在K组链表中反转结点)

原文 给定一个链表,在一定时间内反转这个链表的结点,并返回修改后的链表. 如果结点数不是K的倍数,那么剩余的结点就保持原样. 你不应该在结点上修改它的值,只有结点自身可以修改. 只允许使用常量空间. 例如 给定链表: 1->2->3->4->5 对于 k = 2,你应该返回: 2->1->4->3->5 对于 k = 3,你应该返回: 3->2->1->4->5 翻译 Given a linked list, reverse the

[经典面试题]k节点一组旋转链表

[题目] 给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6. 如果节点的数量是不k的倍数则最终留出节点应该保持原样,每K个一反转,不到k个不用反转.用程序实现. ------美团校招 来自LeetCode :Reverse Nodes in k-Group [代码] #include <iostream> #include <list> using name

[LeetCode]24.Swap Nodes in Pairs

[题目] Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the lis

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

&lt;font color=&quot;red&quot;&gt;[置顶]&lt;/font&gt;

Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸,本博客会持续更新,感谢您的支持,欢迎您的关注与留言.博客有多个专栏,分别是关于 Windows App开发 . UWP(通用Windows平台)开发 . SICP习题解 和 Scheme语言学习 . 算法解析 与 LeetCode等题解 . Android应用开发 ,而最近会添加的文章将主要是算法和Android,不过其它内容也会继续完善. About the Author 独立 Windows App 和

Mapreduce for Machine Learning

MapReduce for Machine Learning Baofeng Zhang 369447122@qq.com  转载请注明出处:http://blog.csdn.net/zbf8441372   Abstract We are at the beginning of the multicoreera. Computers will have increasingly many cores (processors), but there isstill no good program

leetcode难度及面试频率

转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two Pointers 2 Add Two Numbers 3 4 linked list Two Pointers           Math 3 Longest Substring Without Repeating Characters 3 2 string Two Pointers      

LeetCode总结【转】

转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetcode.com的online judge中151道算法题目.除各个题目有特殊巧妙的解法以外,大部分题目都是经典的算法或者数据结构,因此做了如下小结,具体的解题思路可以搜索我的博客:LeetCode题解 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II