本文是在学习中的总结,欢迎转载但请注明出处: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. Java异常处理-----Throwable类

    Throwable类 1.toString() 输出该异常的类名. 2.getMessage() 输出异常的信息,需要通过构造方法传入异常信息(例如病态信息). 3.printStackTrace() ...

  2. Java多线程模型

    谈到Java多线程就涉及到多线程的模型及Java线程与底层操作系统之间的关系.正如我们熟知,现代机器可以分为硬件和软件两大块,如图2-5-1-1,硬件是基础,软件提供实现不同功能的手段.而且软件可以分 ...

  3. FFmpeg的H.264解码器源代码简单分析:宏块解码(Decode)部分-帧间宏块(Inter)

    ===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...

  4. How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer

    How to generate the complex data regularly to Ministry of Transport of P.R.C by DB Query Analyzer 1 ...

  5. Android初级教程:ViewPage使用详解

    转载本博客,请注明出处:http://blog.csdn.net/qq_32059827点击打开链接 ViewPage使用之一就是轮播广告,就以此为出发点,来详细解析一下ViewPage的使用和加载机 ...

  6. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  7. CMake搜索Boost1.57失败及解决

    CMake更新到3.1.0,Boost更新到1.57,结果CMake搜索Boost失败: Unable to find the Boost header files.  Please set BOOS ...

  8. 压力测试工具Ab简介

    Apache安装包中自带的压力测试工具 Apache Benchmark(简称ab) 简单易用,这里就采用 ab作为压力测试工具了. 1.独立安装 通过 yum-utils中的yumdownload  ...

  9. Android Handler机制剖析

    android的handler机制是android的线程通信的核心机制 Android UI是线程不安全的,如果在子线程中尝试进行UI操作,程序就有可能会崩溃. Android中的实现了 接收消息的& ...

  10. springMVC源码分析--容器初始化(一)ContextLoaderListener

    在spring Web中,需要初始化IOC容器,用于存放我们注入的各种对象.当tomcat启动时首先会初始化一个web对应的IOC容器,用于初始化和注入各种我们在web运行过程中需要的对象.当tomc ...