[LintCode] Reverse Linked List 倒置链表
Reverse a linked list.
For linked list 1->2->3
, the reversed linked list is 3->2->1
Reverse it in-place and in one-pass
LeetCode上的原题,请参见我之前的博客Reverse Linked List。
解法一:
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The new head of reversed linked list.
*/
ListNode *reverse(ListNode *head) {
if (!head) return NULL;
ListNode *dummy = new ListNode(-), *cur = head;
dummy->next = head;
while (cur->next) {
ListNode *t = cur->next;
cur->next = t->next;
t->next = dummy->next;
dummy->next = t;
}
return dummy->next;
}
};
解法二:
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The new head of reversed linked list.
*/
ListNode *reverse(ListNode *head) {
if (!head || !head->next) return head;
ListNode *t = head;
head = reverse(t->next);
t->next->next = t;
t->next = NULL;
return head;
}
};
[LintCode] Reverse Linked List 倒置链表的更多相关文章
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- LeetCode 206. Reverse Linked List倒置链表 C++
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. 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-> ...
- [leetcode]206. Reverse Linked List反转链表
Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...
- 206 Reverse Linked List 反转链表
反转一个单链表.进阶:链表可以迭代或递归地反转.你能否两个都实现一遍?详见:https://leetcode.com/problems/reverse-linked-list/description/ ...
- LeetCode206. Reverse Linked List(反转链表)
题目链接:https://leetcode.com/problems/reverse-linked-list/ 方法一:迭代反转 https://blog.csdn.net/qq_17550379/a ...
- 91. Reverse Linked List 反转链表
网址:https://leetcode.com/problems/reverse-linked-list/ 直接参考92:https://www.cnblogs.com/tornado549/p/10 ...
- [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-> ...
随机推荐
- map[C++]
//map是一个存储键值对的容器,也是一个双向链表 #include <iostream> using namespace std; #include <map> int ma ...
- 通过ajax访问Tomcat服务器web service接口时出现No 'Access-Control-Allow-Origin' header问题的解决办法
问题描述 通过ajax访问Web服务器(Tomcat7.0.42)中的json web service接口的时候,报以下跨域问题: XMLHttpRequest cannot load http:// ...
- 在Salesforce中可以对某一个Object的Standard Button或Link进行重写
在Salesforce中可以对某一个Object的Standard Button或Link进行重写,来实现我们特定的逻辑过程,比如:在删除某个Object之前要判断该Object的某个Field的状态 ...
- ios 区域检测 使用coreLocation
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- Java 之 多线程编程
1.线程: a.由来:单任务OS -- 多任务OS b.进程:每一个进程对应一个应用程序,分配独立内存空间 c.线程:线程是进程内部的一个独立的执行分支 d.特点:共享内容地址空间,切换成本更低 2. ...
- supervisor(三)xml_rpc
supervisor提供的两种管理方式,supervisorctl和web其实都是通过xml_rpc来实现的. xml_rpc其实就是本地可以去调用远端的函数方法,然后函数方法经过一番处理后,把结果返 ...
- 在C#程序中实现插件架构
阅读提示:这篇文章将讲述如何利用C#奇妙的特性,实现插件架构,用插件(plug-ins)机制建立可扩展的解决方案. 在.NET框架下的C#语言,和其他.NET语言一样提供了很多强大的特性和机制.其中一 ...
- css随记02布局
布局 二栏布局 利用absolute, margin .container { position: relative; } nav { position: absolute; left: 0px; w ...
- 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1
[虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...
- js三级省市区选择
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...