题目:
原链表为1->2->3->4->5->6->7
k=3的子段逆转结果为
3->2->1->6->5->4->7
code:
关键点
首子段要记录整个链表的头指针,记录相邻两字段的尾节点
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <hash_map>
using namespace std;
typedef struct node
{
int data;
struct node* next;
}*pList,Node;
void rReverse(pList &list,int k)
{
pList p=list, tail, pretail;
tail=p;
int flag=0;
while(p!=NULL)
{
int n=k;
pList post=p->next;
tail=p;
while(post!=NULL && n--!=1)
{
pList temp=post->next;
post->next=p;
p=post;
post=temp;
}
if(post!=NULL && flag==0)
{
list=p;
pretail=tail;
flag=1;
p=post;
}
else if(post!=NULL)
{
pretail->next=p;
pretail=tail;
p=pos