Reverse a Singly LinkedList
 * Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/

这是面试的时候被问到的一题,还是考察LinkedList的理解,最后如何得到reverse之后的head的。

public Class Solution{

    public static ListNode reverseList(ListNode head){
if(head == null || head.next == null){
return head;
} ListNode next = head.next;
head.next = null;
while(next != null){
ListNode temp = next.next;
next.next = head;
head = next;
next = temp;
} return head;
}
}

Reverse a Singly LinkedList的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  2. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  3. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  4. [算法专题] LinkedList

    前段时间在看一本01年出的旧书<effective Tcp/Ip programming>,这个算法专题中断了几天,现在继续写下去. Introduction 对于单向链表(singly ...

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

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

  6. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  7. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  8. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  9. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

随机推荐

  1. Unity截屏

    方式一:直接使用unity自带的截图函数 Application.CaptureScreenshot(“imagename”); 保存路径: 在PC上保存路径为Application.dataPath ...

  2. iOS图片攻略之:有3x自动生成2x 1x图片

       关键字:Xcode插件,生成图片资源 代码类库:其他(Others) GitHub链接:https://github.com/rickytan/RTImageAssets   本项目是一个 Xc ...

  3. MapReduce数据流(二)

    输入块(InputSplit):一个输入块描述了构成MapReduce程序中单个map任务的一个单元.把一个MapReduce程序应用到一个数据集上,即是指一个作业,会由几个(也可能几百个)任务组成. ...

  4. 【IOS基础知识】NSTimer定时器使用

    1.声明 NSTimer         *timer; 2.定义 timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfsele ...

  5. Eclipse中一些快捷键

    用到哪里更新到哪里~~~ 1,代码自动对齐 ctrl+shift+f 2,自动填充相关的包 alt+/   注意选择好需要的包 3,注释某几行代码 选定后ctrl+/ 4,设置自动补全 最简单的修改方 ...

  6. EditTest输入类型设置

    1,当输入为文字性密码时,输入前有提示效果,输入后有遮蔽效果 android:hint="@string/hint_etPassword" android:inputType=&q ...

  7. TCP同步传送数据示例(简洁、清楚)

    转自:http://www.2cto.com/kf/201206/134841.html 本例子写了个简单的TCP数据传送功能.没有使用BinaryWriter,BinaryReader,而是使用Ne ...

  8. jsunit测试

    var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/ba ...

  9. (转)iphone数据存储之-- Core Data的使用

    原文:http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html iphone数据存储之-- Core Data的使用(一)   一. ...

  10. 让多个Fragment 切换时不重新实例化、FragmentTabHost切换Fragment时避免UI重新加载

    http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragment时避免UI重新加载 不过,初次实现时发现有个缺陷,每次FragmentT ...