本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45739753

Reverse a singly linked list.

click to show more hints.

Hint:

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

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

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [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 ...

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

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

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

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

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

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. BeanUtils Exception 之 FastHashMap

    这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了, common logging common collections ··· 在为boolean 这种基本 ...

  2. XMPP(一)-openfire服务端的安装和搭建

    XMPP全称:可扩展通讯和表示协议 简介:可扩展通讯和表示协议 (XMPP) 可用于服务类实时通讯.表示和需求响应服务中的XML数据元流式传输.XMPP以Jabber协议为基础,而Jabber是即时通 ...

  3. memcached实战系列(四)memcached stats命令 memcached优化

    memcached提供一系列的命令进行优化的查看,方便我们调整我们的存储策略,查看我们的使用率,内存的使用率以及浪费情况.常用的命令有stats.stats settings.stats items. ...

  4. AndroidVerifyBoot

    253        Utils.write(image_with_metadata, outPath);254    }227行得到boot.img的size 238行new一个BootSignat ...

  5. quartz 时间设置(定时任务scheduler)

    quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...

  6. 《java入门第一季》之HashSet小案例:获取10个1至20的随机数,要求随机数不能重复

    这是基于HashSet集合的唯一性. /*  * 编写一个程序,获取10个1至20的随机数,要求随机数不能重复.  *   * 分析:  * A:创建随机数对象  * B:创建一个HashSet集合 ...

  7. iOS中 Animation 动画大全 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...

  8. 欢迎进入我的个人博客 anzhan.me

    CSDN的博客依旧会更新,但是还是专注于技术. 个人的博客 http://anzhan.me 不单单会同步csdn的技术文章,还会有个人的更多私人的分享,包括旅行日记.欢迎各位朋友经常去看看,大家有私 ...

  9. 安装配置Kafka

    1,下载kafka安装包,解压缩,tar -zxvf kafka_2.10-0.8.2.1.tgz 2,修改/etc/profile文件,增加KAFKA_HOME变量 3,进入KAFKA_HOME/c ...

  10. ROS_Kinetic_08 ROS的集成开发环境(IDEs)之使用Eclipse

    ROS_Kinetic_08 ROS的集成开发环境(IDEs)之使用Eclipse ROS支持的IDEs比较丰富,这里以Eclipse为例介绍一下. 具体内容参考:http://wiki.ros.or ...