Lintcode35-Reverse Linked List-Easy
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的更多相关文章
- LeetCode--LinkedList--206. Reverse Linked List(Easy)
206. Reverse Linked List(Easy) 题目地址https://leetcode.com/problems/reverse-linked-list/ Reverse a sing ...
- LeetCode_206. Reverse Linked List
206. Reverse Linked List Easy Reverse a singly linked list. Example: Input: 1->2->3->4-> ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [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-> ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【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 ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
随机推荐
- AELF(ELF)区块链项目介绍
AELF(ELF)区块链项目介绍,Aelf在交易所上的名称是ELF,最近涨了不少了,可以长期关注逢低建仓,根据自身情况可以适当轻仓配置点.AELF总结下来就是希望打造一个B2B的区块链开放式OS系统. ...
- Django 应用 静态文件配置
Django 应用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- DOM jquery
DOM 文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM ...
- 垂直打击之JVM剖析
让Java应用程序运行是一回事,但让他们跑得快就是另外一回事了.在面对对象的环境中,性能问题就像来势凶猛的野兽.但JVM的复杂性将性能调整的复杂程度增加了一个级别.这里Refcard涵盖了JVM in ...
- shell 多行注释 块注释
转自 https://www.cnblogs.com/emanlee/p/3749911.html 1 : ' 被注释的多行内容 ' 2 :<<eof 被注释的多行内容 eof 3 :&l ...
- selenium webdriver 实现Canvas画布自动化测试
https://blog.csdn.net/xiaoguanyusb/article/details/80324210 由借鉴意义, 转过来 canvas 是一个画布,定位元素时只能定位到画布上,如下 ...
- ionic + angular + cordova, 打造专属自己的App!
ionic 学习地址:http://ionicframework.com/ ionic 好处: ionic serve --lab 预览平台间的差异化 sass 提 ...
- 哪些个在 Sublime Text 下,"任性的" 好插件!
我在sublime里面安装了以下有利于项目开发高效的插件: 1:SVN 源代码版本控制 2:LiveReload 浏览器实时刷新 3:jsMinifier 压缩 j ...
- 11: python递归
1.1 递归讲解 1.定义 1. 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 2.递归特性 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题 ...
- centos7 yum install timeout
https://yum.dockerproject.org/repo/main/centos/7/repodata/repomd.xml: [Errno 12] Timeout on https:// ...