题目1518:反转链表
2014-11-19 21:15
239 查看
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; struct Node { int num; Node * next ; }; void print(Node * head) { int flag=1; while(head->next) { printf("%d ",head->num); head=head->next; } printf("%d",head->num); } int main() { int n; Node * head=NULL; Node * tail=head; while(~scanf("%d",&n)) { if(n==0) printf("NULL\n"); else { int t; Node * temp=(Node *)malloc(sizeof(Node )); temp->next=NULL; head=temp; tail=head; while(n--) { scanf("%d",&t); temp=(Node *)malloc(sizeof(Node )); temp->num=t; temp->next=head->next; head->next=temp; } print(head->next); printf("\n"); } } return 0; } /************************************************************** Problem: 1518 User: 萧然677 Language: C++ Result: Accepted Time:150 ms Memory:2972 kb ****************************************************************/
逆序输出链表,开始使用递归但发现最后输出时有格式错误,最后的那个空格不知道怎么处理(有大神知道方法可以告诉我哈)。然后改用头插法生成单链表,当建立好链表后此时的链表已经是逆序的。。。
相关文章推荐
- 题目1518:反转链表
- 题目1518:反转链表
- 九度OJ-题目1518:反转链表
- 题目1518:反转链表
- 题目1518:反转链表
- 【剑指Offer面试编程题】题目1518:反转链表--九度OJ
- 剑指Offer题目1518:反转链表
- 题目1518:反转链表
- 题目1518:反转链表
- 【剑指Offer面试编程题】题目1518:反转链表--九度OJ
- 剑指offer-题目1518:反转链表 (2014.1.2)
- 题目1518:反转链表
- 题目1518:反转链表-九度
- 九度_题目1518:反转链表
- 题目1518:反转链表
- 题目1518:反转链表
- 题目1518:反转链表
- 【九度OJ1518】|【剑指offer16】反转链表
- 面试题目-链表反转
- leetcode题目 反转链表系列问题