35. Reverse Linked List

Reverse a linked list.

Example

Example1:
For linked list 1->2->3, the reversed linked list is 3->2->1
Example2:
For linked list 1->2->3->4, the reversed linked list is 4->3->2->1

Challenge

Reverse it in-place and in one-pass

思路:

递归。

1. 定义一个prev成员变量,并初始化为null, 作为反转后链表的头节点

2. 用临时变量保存剩下的(除头结点的)原链表

3. 使取下来的head结点作为反转后链表的头节点(head.next == prev)

4. prev结点指向反转后链表头节点(prev = head)

5. head重新指向剩余链表的头节点。

6. 重复2-6步骤,知道head指向原链表的null位置。

代码:

public ListNode reverse(ListNode head) {
ListNode prev = null; while (head != null) {
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
return prev;
}

Lintcode35-Reverse Linked List-Easy的更多相关文章

  1. LeetCode--LinkedList--206. Reverse Linked List(Easy)

    206. Reverse Linked List(Easy) 题目地址https://leetcode.com/problems/reverse-linked-list/ Reverse a sing ...

  2. LeetCode_206. Reverse Linked List

    206. Reverse Linked List Easy Reverse a singly linked list. Example: Input: 1->2->3->4-> ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

  5. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  6. [LeetCode] 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-> ...

  7. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  8. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  9. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  10. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

随机推荐

  1. 20165305 Linux安装及学习

    一.虚拟机的安装 在根据老师所给的<基于VirtualBox虚拟机安装Ubuntu图文教程>的时候,我发现虚拟化处于被禁用状态,于是我在网上查找了一下解决办法,在我将bios中虚拟化设置为 ...

  2. How to solve the problem that BMW Icom A2 A3 host can’t be connected?

    Aftre the BMW ICOM host is connected to the car via a 16PIN connector, and the other side is connect ...

  3. Linux 用户管理【UID和GID】

    Linux 用户和用户组管理 Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 用户的账号一方面可以帮助 ...

  4. django 数据库html显示

    template {% autoescape off %} {% for each in obj %} <h2>{{ each.food }}</h2> <br/> ...

  5. uwsgi 的巨坑

    网上各种找,最后自己猜,猜到了. 必须安装python插件, 网上找的都是不带数字的版本号, 要么找不到要么不行. 我是 3.6.1,尝试加36, 成了. yum install -y uwsgi-p ...

  6. Linux pip 命令无法使用问题

    Linux pip 命令无法使用问题 pip 命令无法使用,说明 pip 没有安装,我们可以使用终端命令进行安装. sudo apt-get installl python-pip 安装成功之后,可以 ...

  7. 09:vuex组件间通信

    1.1 vuex简介 官网:https://vuex.vuejs.org/zh/guide/ 参考博客:https://www.cnblogs.com/first-time/p/6815036.htm ...

  8. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165211

    目录 软件和镜像下载 虚拟机软件 Kali系统的下载 Kali系统安装 网络配置 设置共享文件夹和剪切板 安装VMware增强工具 设置共享文件夹 设置共享剪切板 更新软件源 软件和镜像下载 虚拟机软 ...

  9. 题说proxy

    昨天在和群友讨论时遇到一题是这样的. 题目描述 //Tomy非常敏感,不喜欢别人碰他的东西.一旦有人碰他就会大喊Don't Touch Me. //完成tomy这个对象,禁止对tomy的内容进行修改( ...

  10. Codeforces 765F Souvenirs - 莫队算法 - 链表 - 线段树

    题目传送门 神速的列车 光速的列车 声速的列车 题目大意 给定一个长度为$n$的序列,$m$次询问区间$[l, r]$内相差最小的两个数的差的绝对值. Solution 1 Mo's Algorith ...