92. Reverse Linked List II
2016-03-09 15:12
330 查看
#include<iostream> #include <cstdio> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* createList(int a[],int n){ ListNode* head=NULL,*rear; for(int i=0;i<n;i++){ ListNode* temp=new ListNode(a[i]); if(head==NULL){ head=temp; rear=head; }else{ rear->next=temp; rear=rear->next; } } return head; } void printList(ListNode* head){ ListNode* pos=head; while(pos){ cout<<pos->val<<" "; pos=pos->next; } } ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode* pos=head; ListNode* mpos,*npos; int count=0,i=0,flag=0; int num[10000]; while(pos){ count++; if(flag==1){ num[i++]=pos->val; } if(count==m) { mpos=pos; num[i++]=pos->val; flag=1; } if(count==n) { npos=pos; break; } pos=pos->next; } pos=mpos; while(pos!=npos->next){ pos->val=num[--i]; pos=pos->next; } return head; } int main(int argc,char *argv[]) { int v[5]={1,2,3,4,5}; ListNode* p=createList(v,5); printList(reverseBetween(p,2,4)); return 0; }
相关文章推荐
- 【SettingsProvider】修改系统初始化时默认时区
- 【翻译】Leapmotion-python开发官方文档(7)
- storm文档(11)----搭建storm集群
- Android中静态成员变量的生命周期
- Maven 打成 webjar的方法
- Git 常用命令大全
- 线段树
- 个体软件过程考试卷
- HashMap和Hashtable的区别
- GIT@OSC中托管Android studio代码
- mybatis 新增判断空
- 19. Remove Nth Node From End of List
- waf实现扫描器识别
- 【bzoj1251】【序列终结者】【splay】
- UIApplication详解(转载)
- 获取文件类型
- storm文档(10)----容错
- 移动应用-使用tcpdump抓包
- 231. Power of Two
- redis configuration