Reverse a singly linked list.

解法一:记录单链表每个节点的val,然后重新为单链表赋值。(取巧,仅仅是将val部分改变,原始node节点并没有改变)

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
List<Integer> list=new ArrayList<Integer>();
ListNode p=head,q=head;
while(p!=null){
list.add(p.val);
p=p.next;
}
int size=list.size();
for(int i=size-1;i>=0;i--){
q.val=list.get(i);
q=q.next;
}
return head; }
}

运行结果:

解法2:迭代。迭代是重复反馈过程的活动,其目的通常是为了逼近所需目标或结果。每一次对过程的重复称为一次“迭代”,而每一次迭代得到的结果会作为下一次迭代的初始值。

2.1 尾插入法。

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode q=head.next;
ListNode r=null;
head.next=null;
while(q!=null){
r=q.next;
q.next=head;
head=q;
q=r;
}
return head; }
}

     运行结果:

解法3:递归实现

代码如下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode p= reverseList(head.next);
ListNode q=p;
while(p.next!=null)
p=p.next;
p.next=head;
head.next=null;
head=q;
return head; }
}

  运行结果:

(easy)LeetCode 205.Reverse Linked List的更多相关文章

  1. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  2. [LeetCode] 92. Reverse Linked List II 反向链表II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  3. Java for LeetCode 092 Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...

  4. [LeetCode] 92. Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  5. [LeetCode] 206. Reverse Linked List 反向链表

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

  6. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  7. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  8. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  9. leetcode -day30 Reverse Linked List II

    1.  Reverse Linked List II  Reverse a linked list from position m to n. Do it in-place and in one- ...

随机推荐

  1. MySQL 加锁处理分析 转

    MySQL 加锁处理分析  转 http://hedengcheng.com/?p=771 十二 13th, 2013 发表评论 | Trackback   1    背景    1 1.1    M ...

  2. linux shell 整理收集(不断更新)

    1)主从复制延时判断 (转 http://www.cnblogs.com/gomysql/p/3862018.html) 说明: 不要通过Seconds_Behind_Master去判断,该值表示sl ...

  3. MySQL 体系结构以及各种文件类型学习汇总 (转)

    1,mysql体系结构 由数据库和数据库实例组成,是单进场多线程架构. 数据库:物理操作系统文件或者其它文件的集合,在mysql中,数据库文件可以是frm.myd.myi.ibd等结尾的文件,当使用n ...

  4. Spring入门学习(一)

    SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...

  5. 【linux】umask

    1.默认情况下的umask值是022 [root@andon ~]# umask 0022  2.umask作用 默认文件权限:666(linux创建文件默认无执行权限)-umask =644(6-0 ...

  6. SPOJ #429 Simple Numbers Conversion

    This is simply a human work simulation - exactly reproducing how you do it by hand. Nothing special. ...

  7. Netdom query基本用法

    C:\Users\user1>netdom queryThe syntax of this command is: NETDOM QUERY [/Domain:domain] [/Server: ...

  8. android学习笔记五——AutoCompleteTextView

    AutocompleteTextview ==> 使用比较容易,只需要为其设置一个Adapter,该Adapter封装其需要预设的文本内容. 如下所示实例: <RelativeLayout ...

  9. 【freemaker】之FreeMakerUtil工具类

    Freemaker生成文件常用工具类 public class FreemakerUtil { private static FreemakerUtil util; private static Co ...

  10. 华人曾与IBM抗衡! 盘点已远去的IT巨头(转)

    [PConline资讯 ]从算盘到计算器,从大型机到个人PC,再到当今火热的移动终端和云计算,人类计算史已经走过了千年之久,现代IT计算领域也经过了百年浮沉.在世界工业领域,IT技术应该是诞生时间最短 ...