LeetCode(206) Reverse Linked List
题目
Reverse a singly linked list.
click to show more hints.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
分析
反转链表。
一个简单的解法,既然反转该链表,我们把所有节点作为一个输入序列,按照头插法重新构造一个链表即可,它既是所给链表的反转结果。
题目所给提示还有另外两种方法解决,迭代和递归。
没有想到所说的迭代是个什么意思,下面将给出头插法和递归实现的代码!
AC代码
class Solution {
public:
//方法一:头插法
ListNode* reverseList(ListNode* head) {
if (head == NULL)
return head;
//反转,即将所有节点按照头插法重新插入一遍即可
ListNode *p = head->next;
head->next = NULL;
while (p)
{
//保存p的后续节点
ListNode *r = p->next;
p->next = head;
head = p;
p = r;
}
return head;
}
//方法二:递归实现
ListNode* reverseList2(ListNode* head) {
if (head == NULL)
return head;
ListNode *p = head;
//反转其余节点组成的链表,将头结点链接到尾部
if (head->next)
{
head = reverseList(head->next);
ListNode *r = head;
while (r->next)
r = r->next;
r->next = p;
}
p->next = NULL;
return head;
}
};
LeetCode(206) Reverse Linked List的更多相关文章
- LeetCode(92) Reverse Linked List II
题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...
- LeetCode(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
- leetcode之旅(9)-Reverse Linked List
题目描述: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed ei ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- LeetCode(151) Reverse Words in a String
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(190) Reverse Bits
题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- Leetcode(206)-反转链表
反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路:反转链表很简 ...
随机推荐
- 线程池(2)Executors.newFixedThreadPool
例子: ExecutorService es = Executors.newFixedThreadPool(5); try { for (int i = 0; i < 20; i++) { Ru ...
- Codeforces Round #377 (Div. 2) E. Sockets
http://codeforces.com/contest/732/problem/E 题目说得很清楚,每个电脑去插一个插座,然后要刚好的,电脑的power和sockets的值相同才行. 如果不同,还 ...
- SpriingMVC执行流程结构
SpringMVC也叫spring web mvc,属于表现层的框架,是Spring框架的一部分. Spring MVC请求流程图: request-------->DispatcherSer ...
- AnyCAD OpenSource 版本下载和编译
下载: SVN下载地址:https://anycad.svn.codeplex.com/svn 或者直接下载:http://anycad.codeplex.com/SourceControl/late ...
- 韦东山笔记之安装arm-linux-gcc交叉编译环境详细步骤。
1在关盘主目录tools下复制arm-linux-gcc-3.4.5-glibc-2.3.6.tar.bz2到虚拟机上 解压 tar xjf arm-linux-gcc-3.4.5-glibc ...
- vue-cli3项目中解决动态引入图片img404的问题
博主最近手头再做一个项目,需要调用天气接口,并且还要动态显示天气相关图片icon. 本来以为没什么大问题,结果硬生生被这个动态图片路径搞得民不聊生(博主还是 too young,too simple~ ...
- bug {was not declared in this scope}
使用自己定义的结构体作为返回值的时候,出现了 ...was not declared in this scope 检查了各种头文件,把缓存也都删掉了还是不行. 结果,发现,应该这样用vector< ...
- NullPointerException检测
APET-NPE插件工作原理 android应用程序编译的过程如下: 从图中,我们可以看出,app编译大致经历了四大阶段:java source files -> .class files -& ...
- codevs 1028 花店橱窗布置
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 假设以最美观的方式布置花店的橱窗,有F束花,V个花瓶,我们用美学值(一个整 ...
- MVC批量上传文件(使用uploadify)
<script src="JS/jquery-1.8.3.js"></script> <script src="uploadify/jque ...