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 ...
随机推荐
- mngoDB 常用语法
http://topmanopensource.iteye.com/blog/1278812### 连接写法:[IP地址:端口号] mongo 192.168.1.161:27017; show db ...
- java 中的 i++ 和 ++i
熟悉c/c++中的i++和++i,那么你知道下面的java代码效果是什么吗? 一 . 代码示例 /** * * @author elelule * */ public class TestPlusPl ...
- 关于javaSocket中 Software caused connection abort: recv failed问题
在学习Socket中今天突然遇到了以下这种问题 原来是网路连接出了问题,由于我測试的是远程连接所以是在学校的局域网下,结果非常不稳定,開始还以为怎么了一会连上了一会又出现故障然后把IP地址改为本机的1 ...
- elasticsearch报错syncedb_path
一般默认syncdb_path在$HOME目录下隐藏文件,也可以自己指定一个文件,记住,这里只能指定文件,不能只写目录input { file { path => "/home/tom ...
- GDBus
1. https://en.wikipedia.org/wiki/D-Bus In computing, D-Bus (for "Desktop Bus"[4]), a softw ...
- scala 遇到过的问题
1:在我安装完scala的插件后,在打开方法的实现类(open implementactions)的时候,抛出这个异常,后来发现这个异常是因为我的scala的插件跟我eclipse版本不兼容导致的. ...
- MapReduce源码分析之新API作业提交(二):连接集群
MapReduce作业提交时连接集群是通过Job的connect()方法实现的,它实际上是构造集群Cluster实例cluster,代码如下: private synchronized void co ...
- 使用js生成下拉列表项
在项目中经常会使用到一个简单的表单元素,那就是下拉列表.但是由于企业的列表项并不是固定的,因此列表项的内容需要到数据库或者接口中取,因此怎样获取,并且能应用到 多个相似的下拉列表就需要思考一下. 我这 ...
- java 对象占用内存查看 以及JVM级别 方法修改等
public interface Instrumentation 此类提供检测 Java 编程语言代码所需的服务.检测是向方法中添加字节码,以搜集各种工具所使用的数据.由于更改完全是进行添加,所以这些 ...
- XSD文件详解
XSD (xml Schema Definition) Xml Schema的用途 1. 定义一个Xml文档中都有什么元素 2. 定义一个Xml文档中都会有什么属性 3. 定义某个节点的都有什么 ...