博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Reverse Nodes in k-Group
阅读量:4541 次
发布时间:2019-06-08

本文共 2797 字,大约阅读时间需要 9 分钟。

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

 

思路: 先实现reverseList(head), 然后实现reverse(start, end)反正从start到end的链表,然后基于reverse(start,end)来完成最后的动作。

 

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {    public:        ListNode *reverse(ListNode* head)        {            if(head == NULL)                return NULL;            ListNode * pre = NULL;            ListNode * cur = head;            ListNode * next = NULL;            // make cur->next = pre;            while(cur)            {                next = cur->next;                cur->next = pre;                pre = cur;                cur = next;            }            return pre;        }#if 1        ListNode* reverse(ListNode* head, ListNode* end)        {            if(head == NULL)                return NULL;            ListNode * pre = NULL;            ListNode * cur = head;            ListNode * next = NULL;            // make cur->next = pre;            while(cur)            {                next = cur->next;                cur->next = pre;                pre = cur;                cur = next;                if(pre == end)                    break;            }            return pre;        }        ListNode *reverseKGroup(ListNode *head, int k)        {            if(head == NULL)                return NULL;            ListNode dummy(-1);            dummy.next = head;            ListNode* pre = &dummy;            ListNode* start= head;            ListNode* end = start;            ListNode* next= NULL;            int cnt = 0;            while(end)            {                cnt++;                if(cnt == k)                {                    next = end->next;                    //assign                    pre->next = reverse(start, end);                    start->next = next;                    //cout << "start\t" << start->val <
val <
next); //update pre = start; start = next; end = next; cnt = 0; //cout << "start\t" << start->val <
val <
val <
next; } return dummy.next; }#endif};

 

转载于:https://www.cnblogs.com/diegodu/p/4280031.html

你可能感兴趣的文章
JS高程第八章 BOM
查看>>
python-vi
查看>>
Unix进程控制
查看>>
DNS解析过程详解
查看>>
51Nod 1181 质数中的质数(质数筛法)
查看>>
并发编程学习总结
查看>>
Xamarin.Android 上中下布局
查看>>
VS Code使用记录
查看>>
locust参数化(数据库取值)
查看>>
Google Protocol Buffers浅析(三)
查看>>
.net core 中使用Google的protoc
查看>>
Spring Cloud和Spring Boot的区别
查看>>
jquery实现图片上传前本地预览
查看>>
C# — 题库答案汇总
查看>>
docker居然需要3.10以上的内核
查看>>
Win10下安装zookeeper
查看>>
客户端用JavaScript填充DropDownList控件,服务器端读不到值
查看>>
Dubbo源码学习--服务是如何引用的
查看>>
【转】C#安装字体到系统
查看>>
Android视频播放之VideoView
查看>>