本文是在学习中的总结,欢迎转载但请注明出处: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. Ant简介

    Ant,apache开源项目,基于Java的构建工具,是一个小程序.它通过自动完成所有的编译代码,运行测试以及 打包重新部署等繁琐费力的任务来帮助软件团队开发大程序: Ant的目标是自动完成所有的构建 ...

  2. JAVA面向对象-----抽象类

    1抽象类 为什么使用抽象类 1:定义Dog类 有颜色属性和叫的方法 2:定义Bird类 有颜色属性和叫的方法 3:定义其父类Animal 1:颜色的属性可以使用默认初始化值. 2:叫的方法在父类中如何 ...

  3. dos2unix批量转换的一种方法

    Linux本身提供了dos2unix和unix2dos两个命令来实现Windows和Linux文件的转换. 少量文件转换: 对于单个或少量的文件转换,可以直接使用命令,如: dos2unix file ...

  4. FFmpeg源代码简单分析:av_write_trailer()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  5. Dynamics Crm 2011 Or 2013 IFD 部署一段时间后,CA验证问题

    以下错误描述摘自博客:http://blog.csdn.net/qzw4549689/article/details/14451257 IFD部署一段时间后,大概一年,突然出现从IFD登录页面登录后, ...

  6. (一〇四)使用Xcode6创建framework动态静态库

    在Xcode6以前,创建framework可以使用iOS-Universal-Framework模板来创建framework,现在苹果已经提供了模板,如下图选择: 使用此模版创建的默认是动态库,方法和 ...

  7. UNIX网络编程——UDP缺乏流量控制(改进版)

    现在我们查看无任何流量控制的UDP对数据报传输的影响.首先我们把dg_cli函数修改为发送固定数目的数据报,并不再从标准输入读.如下,它写2000个1400字节大小的UDP数据报给服务器. 客户端程序 ...

  8. python访问redis

    python访问redis 1 Linux上安装redis a) 下载 $ wget http://download.redis.io/releases/redis-3.0.5.tar.gz b) 编 ...

  9. J2EE学习从菜鸟变大鸟之七 Servlet

    Servlet现在自己的理解是一个控制器,简单的可以理解为不同的JSP页面由用户发送过来的请求可以由Servlet控制器来控制其向下调用的方向(结合三层好理解),但它比较特殊,因为它通常会从外界接收数 ...

  10. js对象、构造函数、命名空间、方法、属性

     <script language="javascript">   var myNameSpace = new Object(); //构造一个命名 空间myCla ...