【剑指offer之反转链表】九度OJ-1518-反转链表
来源:互联网 发布:2013旧版qq软件 编辑:程序博客网 时间:2024/05/29 18:35
【题目链接】:九度OJ-1518-反转链表
【题目描述】:
输入一个链表,反转链表后,输出链表的所有元素。
(hint : 请务必使用链表)
输入:
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000):代表将要输入的链表的个数。
输入的第二行包含n个整数t(0<=t<=1000000):代表链表元素。
输出:
对应每个测试案例,
以此输出链表反转后的元素,如没有元素则输出NULL。
样例输入:
5
1 2 3 4 5
0
样例输出:
5 4 3 2 1
NULL
【思路】:
【代码】:
/********************************** Date:2017-04-15 13:50* Author:herongwei* Problem: 题目1518:反转链表* Source:http://ac.jobdu.com/problem.php?pid=1518 【剑指Offer 】* Result:AC**********************************/#pragma comment(linker,"/STACK:102400000,102400000")#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <set>#include <stack>#include <math.h>#include <map>#include <queue>#include <deque>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5+10;const int maxm = 55;const LL MOD = 999999997;int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};inline int read(){ int c=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();} return c*f;}typedef struct Listnode{ int value; struct Listnode *next;} Listnode;///////////////////////////////////////////////////////////////////////// Reverse a list iteratively// Input: pHead - the head of the original list// Output: the head of the reversed head///////////////////////////////////////////////////////////////////////Listnode* ReverseList(Listnode* phead){ ///容错处理 if(phead == NULL || phead->next == NULL) return phead; ///带头结点的链表 else { Listnode *pCurr=phead->next,*pPre=NULL,*pNext; while(pCurr != NULL) { pNext = pCurr->next; if(pNext == NULL) phead=pCurr; pCurr->next = pPre; pPre = pCurr; pCurr= pNext; } return phead; }}void Creat_and_Print_List(int n){ Listnode *phead, *pCurr,*pPre; ///创建链表 phead = (Listnode*)malloc(sizeof(Listnode)); phead->next = NULL; pPre = phead; for(int i=0; i<n; ++i) { pCurr = (Listnode*)malloc(sizeof(Listnode)); pCurr->value=read(); pCurr->next = NULL; pPre->next = pCurr; pPre=pCurr; } if(n<=0) printf("NULL\n"); else { phead = ReverseList(phead); pCurr = phead; while(pCurr != NULL) { if(pCurr->next == NULL) { printf("%d\n",pCurr->value); } else { printf("%d ",pCurr->value); } pCurr = pCurr->next; } }}int main(){ //freopen("in.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF) { Creat_and_Print_List(n); } return 0;}
1 0
- 【剑指offer之反转链表】九度OJ-1518-反转链表
- 九度oj-1518-反转链表
- 【剑指Offer面试编程题】题目1518:反转链表--九度OJ
- 【剑指Offer面试题】 九度OJ1518:反转链表
- 九度OJ-题目1518:反转链表
- 剑指Offer之反转链表
- 剑指Offer之 - 反转链表
- 剑指offer之反转链表
- 【剑指offer】之反转链表
- 剑指offer(十二)之反转链表
- 剑指offer之反转链表
- 剑指offer 之 反转链表
- 剑指offer之十四---反转链表
- 剑指offer:反转链表
- 剑指offer--反转链表
- 剑指offer--反转链表
- 剑指Offer-反转链表
- 《剑指offer》反转链表
- Git版本管理工具的学习
- 【357】Count Numbers with Unique Digits
- NYOJ 小珂的烦恼
- 日常收集有用的网址:
- spring整合struts2和hibernate在删除关联数据的时候特别注意
- 【剑指offer之反转链表】九度OJ-1518-反转链表
- JAVA基本类型与包装类
- 【C++学习笔记】递增/递减运算符和指针
- 继承中的public,protected,private
- windows系统与虚拟机下的linux系统相互传输文件
- c++ primer 第2章 变量和基本类型
- 置换 置换群 应用
- The request sent by the client was syntactically incorrect
- hdu1455 Sticks(搜索+剪枝+剪枝+.....+剪枝)