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 ...
随机推荐
- Linux中查看文本文件内容命令cat/tac/nl/more/less/head/tail/vi总结
概述 在Linux系统下,有很多命令可以查看文本文件的内容,如cat/tac/nl/more/less/head/tail等命令,当然还有vi/nano等文本编辑器.在这里,我只介绍其中自己常用的一部 ...
- 【SSH系列】-- Hibernate持久化对象的三种状态
在上一篇博文中,小编主要简单的介绍了[SSH系列]--hibernate基本原理&&入门demo,今天小编来继续介绍hibernate的相关知识, 大家知道,Java对象的生命周期,是 ...
- 安卓开发:简单的登陆跳转_APK实现直接跳转到本CSDN博客
最近在开始接触Android APP开发,有了一点java基础之后,安卓代码确实看起来就没有那么难了,可以跟着书上把例程敲一遍,然后熟能生巧可以应用起来,现在写了一个简单的APP,实现的是Edit编辑 ...
- 使用std::vector优化点云动画显示一例
1. 准备 使用std::vector应该知道几点: (1)内存连续的容器,有点像数组 (2)与std::list相比,插入和删除元素比较慢- 因为数据迁移 (3)添加元素可能会引发内存分配和数据迁移 ...
- Linux之dmesg命令
功能说明:显示内核缓冲区系统控制信息的工具 ,比如系统在启动时的信息会写到/var/log/中.语 法:dmesg [-cn][-s <缓冲区大小>] 补充说明:kernel会将开机信息存 ...
- dbcp连接池不合理的锁导致连接耗尽
应用报错,表象来看是连接池爆满了. org.springframework.transaction.CannotCreateTransactionException: Could not open J ...
- android 使用Vysor投影到电脑
有没有好的投影软件可以将android屏幕投影到电脑,当然这种很多,比如360就自带了投影功能,小米盒子也可以(不过貌似只能支持到4.4版本),今天要说的是Vysor,google的一款投影软件. V ...
- Android 5.1 添加硬件抽象层(HAL)和JNI接口总结
点击打开链接
- Day 21:Docker 入门教程
几个月以前,红帽(Red Hat)宣布了在 Docker 技术上和 dotCloud 建立合作关系.在那时候,我并没有时间去学习关于 Docker 的知识,所以在今天,趁着这个 30 天的挑战,我决定 ...
- 查全率(召回率)、精度(准确率)和F值
文献中的recall rate(查全率或召回率) and precision(精度)是很重要的概念.可惜很多中文网站讲的我都稀里糊涂,只好用google查了个英文的,草翻如下:召回率和精度定义: 从一 ...