Leetcode_206_Reverse Linked List
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45739753
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
思路:
(1)题意为给定链表,将其(逆置)翻转。该题在校招笔试面试中出现频率较大。
(2)该题主要考察对链表中指针的考察,其实并不难。首先,考虑设定两个指针fis和sed,分别指向链表的第一个元素和第二个元素,其中fis指向结果链表,需在此将其next置为null;其次,循环对指向第二个元素的sed为空进行判定,如果不为空,则设定指针thd指向第三个元素,将sed的next指向fis,这样就完成了前两个元素的逆置;最后,需将fis和sed的同步后移动,而此时的thd就起到了保存位置信息的作用,移动后得到fis指向sed所在位置,sed指向thd所在位置,这样循环遍历,最终得到的fis即为结果链表。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/**
*
* @param liqq
* @return
*/
public ListNode reverseList(ListNode head) {
if (head == null)
return null;
if (head != null && head.next == null)
return head;
ListNode fis = head;
ListNode sed = head.next;
fis.next = null;
while (sed != null) {
ListNode thd = sed.next;
sed.next = fis;
fis = sed;
sed = thd;
}
return fis;
}
Leetcode_206_Reverse Linked List的更多相关文章
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- PHP学习(2)——运行环境搭建
学习PHP首先要搞定PHP的运行环境.PHP的运行环境包括:PHP语言解析器本身以及Apache服务器.MySQL数据库等.因为只是学习嘛,尽快的搭建起来运行环境就好,到后期慢慢懂得多了再去想规范化搭 ...
- ghmm在 Linux 上安装
ghmm在 Linux 上安装 http://ghmm.sourceforge.net/documentation.html http://www.ghmm.org http://www.comp.l ...
- 如何将dtb反编译成dts
点击打开链接 由于device tree会将一个node的信息分布在各个文件里,查看起来很不方便,比如如下例子,ldb在三个文件中都有配置: imx6qdl-sabresd.dtsi: [plain] ...
- 1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
1 新建项目 A 修改pro中的内容如下: HEADERS += \ MyWidget.h SOURCES += \ MyWidget.cpp QT += gui widgets prints ...
- Handler,MessageQueue Loop 和Message的原理解析
先介绍和handler一起工作的几个组件 Handler的方法介绍 代码示例 package liu.peng.weather; import java.util.Timer; import java ...
- Java进阶(三十五)java int与integer的区别
Java进阶(三十五)java int与Integer的区别 前言 int与Integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而Integer是对象 ...
- Hessian探究(一)Hessian入门示例
一.hessian的maven信息: [html] view plain copy print? <dependency> <groupId>com.caucho</gr ...
- Android性能优化之Splash页应该这样设计
目前SplashActivity的设计 目前市场上的应用在启动时基本上都会先启动一个SplashActivity,作为一个欢迎界面,为什么这样设计呢? 个人总结有三个优点: 1.可以给用户更好的体验 ...
- 使用Python做简单的字符串匹配
由于需要在半结构化的文本数据中提取一些特定格式的字段.数据辅助挖掘分析工作,以往都是使用Matlab工具进行结构化数据处理的建模,matlab擅长矩阵处理.结构化数据的计算,Python具有与matl ...
- 存储那些事儿(二): 下一代Linux文件系统BTRFS简介
BTRFS,通常念成 Butter FS,Better FS 或B-tree FS.下一代的Linux文件系统. 它基于写时拷贝(copy-on-write),支持高效的snapshot和clone. ...