Data Structure Linked List: Write a function to reverse a linked list
iterative太简单不写了
http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node *head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
} void reverselist(node *&head) {
if (!head) return;
node *cur = head;
node *next = head->next;
if (!next) return;
reverselist(next);
cur->next->next = cur;
cur->next = NULL;
head = next;
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
print(head);
reverselist(head);
print(head);
return ;
}
Data Structure Linked List: Write a function to reverse a linked list的更多相关文章
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- [leetcode]432. All O`one Data Structure全O(1)数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...
随机推荐
- SQL的四种连接
SQL的四种连接-内连接.左外连接.右外连接.全连接 今天在看一个遗留系统的数据表的时候发现平时查找的视图是FULL OUT JOIN的,导致平时的数据记录要进行一些限制性处理,其实也可以设置视图 ...
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...
- QT项目性能调优小记
最近的项目用到了QT 5.5,项目在运行过程中出现了一段时间CPU占用率持续25%,并频繁断网的情况,遂决定对项目性能进行优化. 优化工具也是VS2010自带的性能分析工具,具体的使用方法参见:htt ...
- vim-colors-config
在vim中,主题是以插件形式存在.其中系统自带的主题,存放在$VIMRUNTIME/colors文件夹下,以*.vim命名.(注:查看$VIMRUNTIME请在vim中执行 :echo $VIMRUN ...
- LeetCode 206. Reverse Linked List(迭代和递归两种实现)
递归的代码比迭代的代码看起来更清爽一些,也是由于递归对行为进行了抽象吧. 注意到,这是一个尾递归函数.一些编译器会将它优化为迭代,这样一方面,在代码层面保持了清晰的逻辑和可读性.一方面保持了代码的性能 ...
- 深入了解Erlang 垃圾回收机制以及其重要性(转)
声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...
- Hadoop学习笔记——Hadoop经常使用命令
Hadoop下有一些经常使用的命令,通过这些命令能够非常方便操作Hadoop上的文件. 1.查看指定文件夹下的内容 语法: hadoop fs -ls 文件文件夹 2.打开某个已存在的文件 语法: h ...
- [Android] 拍照、截图、保存并显示在ImageView控件中
近期在做Android的项目,当中部分涉及到图像处理的内容.这里先讲述怎样调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买 ...
- VC++ 读写注冊表,注冊文件图标关联
#include <string> #include <iostream> #include <Windows.h> #include <shlobj.h&g ...
- Java学习笔记——java介绍
Java开源语言 C语言闭源语言 IOS闭源系统 采用object-c语言开发 应用程序分类(从类型分类) C/S(Client Server):不联网的软件也属于C/S B/S(Browser S ...