206. Reverse Linked List【easy】

Reverse a singly linked list.

Hint:

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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  4. 234. Palindrome Linked List【Easy】【判断链表是否回文】

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. 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 ...

  6. 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 ...

  7. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Codeforces #480 Tutorial

    Problem A,B,C: 简单的模拟,注意A中p mod q时对q=0特殊处理(注意范围) Problem D: Brief Intro: 给定长度为N的数组A,将A中所有连续子序列分成最少的组, ...

  2. 求满足n^2>12000的n的最大值 Exercise05_13

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求满足n^2>12000的n的最大值 * */ public class Exercise05_13 { public ...

  3. C#Windows服务:一些方法(启动、停止等)

    前面讲述了如何创建和安装服务(创建windows服务),下面把启动.停止.判断是否启动的方法也写一下. /// <summary> /// 判断是否安装了某个服务 /// </sum ...

  4. iOS 多线程之NSOperation篇举例详解

    这篇博客是接着总篇iOS GCD NSOperation NSThread等多线程各种举例详解写的一个支篇.总篇也包含了此文的链接.本文讲解的知识点有NSBlockOperationClick,队列, ...

  5. [Git] Git把Tag推送到远程仓库

    转载: http://blog.csdn.net/hustpzb/article/details/8056518 用git tag来给工程打上标签,但是这个命令只是在本地仓库打标签而已, 为了能把标签 ...

  6. C++之共有继承、保护继承、私有继承

    1.封装,public,private作用就是这个目的. 类外只能访问public成员而不能方位private成员: private成员只能被类成员和友元访问: 2.继承,protected的作用就是 ...

  7. nagios学习记录

    这几天开始接触nagios,记录下学习的心得 监控机上需要安装nagios,nagios-plugins, nrpe 被监控机上需要安装nagios-plugins, nrpe nagios通过插件n ...

  8. 第五章:关于ESearch的应用

    ElasticSearch是一个开源的分布式搜索引擎,具备高可靠性,支持非常多的企业级搜索用例. 我在这里做一下简单介绍和整理: 首先.ES搜索引擎给予java开放使用之前必须安装java环境,使用j ...

  9. javascript => 方法的简写形式

    https://segmentfault.com/a/1190000002904199 => 是function的简写形式,支持expression 和 statement 两种形式.同时一点很 ...

  10. jquery ajax/post 请求 案例

    @RequestMapping("/hello")    @ResponseBody    public Hello getJson(HttpServletRequest requ ...