206 - Reverse Linked List
2016-03-10 10:44
302 查看
Reverse a singly linked list.
click to show more hints.
Subscribe to see which companies asked this question
思路分析:
此题较容易。
我们可以设置一个新的指针newHead。初始化为NULL。
此时,迭代原始的链表。每一次把原始的链表的节点从头插入newHead中。
假定初始链表为:1->2->3->4->5
1. newhead->1->null
2. newhead->2->1->null
...
5 newhead->5->4->3->2->1->null
Then just return newhead->next.
click to show more hints.
Subscribe to see which companies asked this question
思路分析:
此题较容易。
我们可以设置一个新的指针newHead。初始化为NULL。
此时,迭代原始的链表。每一次把原始的链表的节点从头插入newHead中。
假定初始链表为:1->2->3->4->5
1. newhead->1->null
2. newhead->2->1->null
...
5 newhead->5->4->3->2->1->null
Then just return newhead->next.
#include "stdafx.h" #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution_206_ReverseLinkedList { public: ListNode* reverseList(ListNode* head) { ListNode *newHead = new ListNode(0); while (head) { ListNode *tmp = head->next; newHead->next = head; head = head->next; newHead->next->next = tmp; } return newHead->next; } };
相关文章推荐
- queue —— 队列使用示例
- maven下载jar包不完全解决办法
- C++第一次实验(2)
- TestBird刘承杨:影响智能硬件的用户体验的一个关键环节
- socket 极值数量
- Eclipse集成Tomcat的配置步骤实例
- 技术干货 | Docker容器中需要避免的十种常见误区
- An Awful Problem
- XNA4.0 RPG游戏开发教程(六)
- 机器学习算法基础概念学习总结
- c++第一次实验
- c++第一次实验-1
- 51单片机定时器的使用
- 有哪些老鸟程序员知道而新手不知道的小技巧?
- Minimum Inversion Number
- ios 视频/图片压缩
- 补充记录--获取日期时间,当前时间
- Android开发:最详细的 NavigationDrawer 开发实践总结
- 案例一:HelloAndroid
- HDU 4126 Genghis Khan the Conqueror 类似于4756的树形DP