Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]
2016-03-03 20:03
483 查看
题目:
Reverse a singly linked list.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
翻译:
反转一个单链表。
分析:
可以先尝试通过简单例子画图分析,来弄清如何修改指向。
代码:
Reverse a singly linked list.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
翻译:
反转一个单链表。
分析:
可以先尝试通过简单例子画图分析,来弄清如何修改指向。
代码:
[code]/** * 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){ return null; } ListNode nextNode=null; ListNode currentNode=head; ListNode lastNode=null; while(true){ nextNode=currentNode.next; currentNode.next=lastNode; if(nextNode==null){ break; } lastNode=currentNode; currentNode=nextNode; } return currentNode; } }
相关文章推荐
- 采用nginx让多个tomcat实现负载均衡
- JS练习(个人练习有许多问题,解决不了)
- mac 安装lua命令
- Java对象的多态性
- 做一个合格的程序猿之MYSQL存储引擎INNODB简介之非锁定读(二)
- 动画帧加载过程
- 支持向量机--Hard-Vargin Support Vector Machine
- hdu 4734 F(x)
- 创业笔记-Node.js入门之基于事件驱动的回调
- XML和JSON
- 三层架构详解
- 数组的简单方法
- 自学 java 笔记 day6(面向对象2)
- hadoop2.x 动态添加节点
- [转载]Markdown——入门指南
- 【CodeVS2039】骑马修栅栏
- Generate Parentheses
- 创业笔记-Node.js入门之一个完整的基于Node.js的web应用
- python:删除.svn文件
- linux