206. Reverse Linked List【easy】
206. Reverse Linked List【easy】
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
解法一:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * pre = NULL;
while (head != NULL) {
ListNode * next = head->next;
head->next = pre;
pre = head;
head = next;
}
return pre;
}
};
解法二:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};
参考了@jianchao.li.fighter 的代码
The basic idea of this recursive solution is to reverse all the following nodes after head. Then we need to set head to be the final node in the reversed list. We simply set its next node in the original list (head -> next) to point to it and sets its next to be NULL.
解法三:
public ListNode reverseList(ListNode head) {
/* recursive solution */
return reverseListInt(head, null);
}
private ListNode reverseListInt(ListNode head, ListNode newHead) {
if (head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}
参考了@braydenCN 的代码
206. Reverse Linked List【easy】的更多相关文章
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 234. Palindrome Linked List【Easy】【判断链表是否回文】
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...
- 876. Middle of the Linked List【Easy】【单链表中点】
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
随机推荐
- 【高斯消元】【异或方程组】poj1222 EXTENDED LIGHTS OUT
由于每个点的状态受到其自身和周围四个点的影响,所以可以这样建立异或方程组: 引用题解: http://hi.baidu.com/ofeitian/item/9899edce6dc6d3d2974452 ...
- Problem Z: 零起点学算法22——求正弦和余弦
#include<stdio.h> #include <math.h> int main() { int n; ); double a,b; while(scanf(" ...
- 【MySQL笔记】: unable to connect to remote host. catalog download has failed.
安装完MySQL之后,它每天凌晨启动一个Intaller任务,甚是烦人: 这是一个Windows的计划服务,在这里删除即可,开始/附件/系统工具/任务计划程序,把mysql的定时任务计划取消/删除 ...
- Java高级架构师(一)第03节:多模块多Web应用合并War包
多模块.多Web应用合并war包 在日常的系统开发中,如果担心各个系统的资源同名覆盖,可以在总的War模块下放置一份最终的资源. 将版本号改成9.1.0.v20131115,ok 在Idea中的Mav ...
- 在xcode6中使用矢量图(iPhone6置配UI)
转载出处:http://blog.xoneday.com ios应用程序是一个图像主导的产品.在开发一个应用程序时,你需要各种尺寸的图标,你需要为每个图像文件制作一个@1x尺寸和一个@2x尺寸.这样你 ...
- NHibernate官方文档中文版--只读实体类型(Read-only entities)
重点 NHIbernate处理只读对象的方式可能和你在别处看到的不同.不正确的使用方式可能造成不可预料的结果. 当一个实体是只读的时候: NHIbernate不会对实体的简单属性和单向关联数据检查数据 ...
- 8、面向对象class
对象的概念同其他语言的对象相同 一个基本的类 #!/usr/bin/python class person: def hi(self,name): print 'Hello,%s'%name p1= ...
- iOS:iOS为什么要用-all_load、-ObjC、-force_load
为了减少工作量复用部分代码,于是乎我们开始选择重构整个项目,把可以公用的代码放在一起打包成一个静态库导入到其他的项目中使用. 介绍这部分内容的文章在网上很多,各位可以Baidu一下细看. 但是每次在加 ...
- iOS:二维码的生成
所谓的二维码就是一个图片,只不过在iOS需要借用<CoreImage/CoreImage.h>来实现, 并且二维码图片是通过CIImage来转成UIImage的.具体步骤如下: // 1 ...
- solr6.6 高级搜索Facet
1.介绍 facet分面查询是solr中以导航为目的的查询,在用户查询的结果上根据分类增加了count信息,然后用户根据count信息做进一步实现渐进式精确搜索. 什么字段适合用facet呢? fa ...