做模版网站,wordpress比较慢,做资源网站怎么不封,wordpress 08一、题目 已知一个链表的头部head#xff0c;每k个结点为一组#xff0c;按组翻转。要求返回翻转后的头部
k是一个正整数#xff0c;它的值小于等于链表长度。如果节点总数不是k的整数倍#xff0c;则剩余的结点保留原来的顺序。示例如下#xff1a;
#xff08;要求不…一、题目 已知一个链表的头部head每k个结点为一组按组翻转。要求返回翻转后的头部
k是一个正整数它的值小于等于链表长度。如果节点总数不是k的整数倍则剩余的结点保留原来的顺序。示例如下
要求不可以仅仅改变节点内部的值而是真正的交换节点 二、解题思路 1.首先每次检查剩余未翻转的节点是否满足k个如果不满足则直接返回。 2.如果满足k个将其取出写一个独立函数对其翻转并返回翻转后的头尾指针。 3.再根据头尾指针将子表连接回原表中继续往下重复步骤1。
注意在取出子表之前需保存好它在原表中的头尾指针这样翻转后才能连接回原表
三、代码
#include iostreamusing namespace std;struct ListNode {int val;ListNode* next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode* next) : val(x), next(next) {}
};//展示链表节点顺序
void showList(ListNode* head) {bool first true;while (head) {if (first) {first false;cout head-val;} else {cout - head-val;}head head-next;}cout endl;
}//创造链表
ListNode* createList(int count) {ListNode* head new ListNode(1);ListNode* p head;for (int i 2; i count; i) {p-next new ListNode(i);p p-next;}p-next nullptr;return head;
}//翻转链表并返回头尾
pairListNode*, ListNode* myReverse(ListNode* head, ListNode* tail) {ListNode* prev tail-next;ListNode* p head;while (prev ! tail) {ListNode* next p-next;p-next prev;prev p;p next;}return { tail, head };
}//按k个为一组翻转链表
ListNode* reverseKGroup(ListNode* head, int k) {//做一个头节点ListNode* hair new ListNode(0);hair-next head;ListNode* pre hair;while (head ! nullptr) {ListNode* tail pre;//判断剩余节点是否够k个for (int i 0; i k; i) {tail tail-next;if (!tail) {return hair-next;}}ListNode* next tail-next;pairListNode*, ListNode* res myReverse(head, tail);head res.first;tail res.second;//将翻转后的子链表接回去pre-next head;tail-next next;//准备下一组翻转pre tail;head tail-next;}return hair-next;
}//主函数
int main() {ListNode* head createList(5);cout Before reverse by 2 : endl;showList(head);//按2个为一组翻转链表ListNode* rev_head reverseKGroup(head, 2);cout endl endl;cout Before reverse by 2 : endl;showList(rev_head);return 0;
}
四、执行结果
文章转载自: http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.lmqw.cn.gov.cn.lmqw.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.nwpnj.cn.gov.cn.nwpnj.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.mtmnk.cn.gov.cn.mtmnk.cn http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn http://www.morning.fengnue.com.gov.cn.fengnue.com http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.pphgl.cn.gov.cn.pphgl.cn http://www.morning.rkqkb.cn.gov.cn.rkqkb.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.rbnj.cn.gov.cn.rbnj.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.rhkq.cn.gov.cn.rhkq.cn http://www.morning.hxcuvg.cn.gov.cn.hxcuvg.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.pqjpw.cn.gov.cn.pqjpw.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.jtdrz.cn.gov.cn.jtdrz.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.wdwfm.cn.gov.cn.wdwfm.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.xppj.cn.gov.cn.xppj.cn http://www.morning.ggnjq.cn.gov.cn.ggnjq.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.spqbp.cn.gov.cn.spqbp.cn http://www.morning.lfqnk.cn.gov.cn.lfqnk.cn