每天一道LeetCode--206. Reverse Linked List
Reverse a singly linked list.
package cn.magicdu; import cn.magicdu.extra.ListNode; public class _206_Reverse_Linked_List {
/**
* 迭代
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode l = new ListNode(0);
l.next = head;
ListNode pre = head;
ListNode p = pre.next;
while (p != null) {
pre.next = p.next;
p.next = l.next;
l.next = p;
p = pre.next;
}
return l.next;
} /**
* 递归
* @param head
* @return
*/
public ListNode reverseList1(ListNode head) {
if(head==null || head.next==null)
return head;
ListNode nextNode=head.next;
ListNode newHead=reverseList(nextNode);
nextNode.next=head;
head.next=null;
return newHead;
}
}
每天一道LeetCode--206. Reverse Linked List的更多相关文章
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- [LeetCode] 206. Reverse Linked List_Easy tag: Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- LeetCode 206. Reverse Linked List倒置链表 C++
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
随机推荐
- 【Android】Handler的应用(三):从服务器端分页加载更新ListView
在前面两节中,我们了解了如何从服务器中加载JSON数据. 现在,我们将把服务器中的JSON数据加载更新到ListView. 并且,结合之前博文的 “动态追加分页ListView数据”的相关知识,实现 ...
- Spring REST实践之HATEOAS
HATEOAS HATEOAS(The Hypermedia As The Engine Of Application Statue)是REST架构的主要约束."hepermedia&quo ...
- st_Alarm_GenAlarmDealTime
USE [ChiefmesNew]GO/****** Object: StoredProcedure [dbo].[st_Alarm_GenAlarmDealTime] Script Date: 04 ...
- Enhancing the Scalability of Memcached
原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...
- cocos2d-x 获取系统时间
转自:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long getCurren ...
- 中国软件开发project师之痛
在最近的一次会议上,有高层谈到之前在中国觉得自己做得非常牛,但与美国同行接触后却发现与人家存在非常大的差距,这一点我在外企工作时也有过相同的体会.真正与外国同行接触后才会知道什么是差距,在这篇文章中我 ...
- android访问webservices
/** * 手机号段归属地查询(模拟器.HTC 可以) * * @param phoneSec 手机号段 */ public void getRemoteInfo() { /*String pho ...
- Android 滑动效果高级篇(八)—— 自定义控件
自定义控件,较常用View.ViewGroup.Scroller三个类,其继承关系如下: 本示例自定义控件,实现一个Gallery效果,并添加了一个显示View个数和位置的bar条,效果图: 自定义控 ...
- curl要注意的几点
1.post提交数据 $postData = array( 'paramCity' => array(array('id' => $city_id, 'day' => $city_d ...
- 关于null和undefined
null和undefined都是一种类型..typeof查看变量类型.不要为该函数迷惑..因为他只是看上去官方! 见http://www.cnblogs.com/zhepama/articles/30 ...