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

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

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.

 

源码:

1 #include 
2 using namespace std; 3 4 ListNode *reverse(ListNode *head,ListNode *last_tail){ 5 ListNode *next_node=head->next; 6 ListNode *res=head; 7 8 while(next_node){ 9 ListNode *tmp=next_node->next;10 next_node->next=head;11 head=next_node;12 next_node=tmp;13 }14 15 last_tail->next=head;16 return res;17 }18 19 ListNode* reversekGroup(ListNode *head,int k){20 ListNode *newHead=new ListNode(0);21 newHead->next=head;22 23 int cnt=0;24 ListNode *cur_node=head;25 ListNode *last_tail=newHead;26 while(cur_node){27 cnt++;28 if(cnt==k){29 ListNode *tmp=cur_node->next;30 cur_node->next=0;31 32 last_tail=reverse(last_tail->next,last_tail);33 last_tail->next=tmp;34 35 cnt=0;36 continue;37 }38 cur_node=cur_node->next;39 }40 return newHead->next;41 }

 

 

 

转载于:https://www.cnblogs.com/sixue/p/4014594.html

你可能感兴趣的文章
Windows 2003+IIS6+PHP5.4.10配置PHP支持空间的方法(转)
查看>>
去除express.js 3.5中报connect.multipart() will be removed in connect 3.0的警告(转)
查看>>
Android WIFI 无缝切换 小结(1)
查看>>
BZOJ 5194--[Usaco2018 Feb]Snow Boots(STL)
查看>>
BS系统开发历程
查看>>
asp.net 设置回车的默认按钮 (转载)
查看>>
Palindrome Partitioning
查看>>
Microservice架构模式简介
查看>>
换种形式工作
查看>>
javascript中三种典型情况下this的含义
查看>>
Python学习笔记day2(python基础一)
查看>>
【QC】安装
查看>>
628. Maximum Product of Three Numbers
查看>>
log4j Spring aop 注解的日志管理
查看>>
Spring cloud实战 从零开始一个简单搜索网站_Hystrix断路由的实现(三)
查看>>
Android服务Service
查看>>
sqlalchemy学习(一)
查看>>
silverlight Image Source URI : 一个反斜杠引发的血案
查看>>
Windows Phone开发(35):使用Express Blend绘图 转:http://blog.csdn.net/tcjiaan/article/details/7493010...
查看>>
Windows Phone开发(33):路径之其它Geometry 转:http://blog.csdn.net/tcjiaan/article/details/7483835...
查看>>