//逆转链表http://blog.163.com/lichunliang1988116@126/blog/static/26599443201282083655446/
#include<iostream.h>
#include<stdlib.h>
typedef struct Node
{
int data;
Node *next;
}*Linklist,ListNode;
void initLink(Linklist *head)
{ Node *node=(Node *)malloc(sizeof(Node));
node->data=;
node->next=NULL; head=&node; } void addNode(Linklist head,int no) {
Node *node=(Node *)malloc(sizeof(Node));
node->data=no;
node->next=NULL;
cout<<"加个数据为"<<no<<endl;
node->next=head->next;
head->next=node;
} void print(Linklist head)
{
Linklist current=head;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next; }
cout<<endl; }
//recursive fanhui head function
Linklist recursive2(Linklist head,Linklist &newHead)
{
if(head->next==NULL)
{
newHead=head;
return head; }
Node *p1=head;
Node *p2; p2=recursive2(p1->next,newHead); p2->next=p1;
p1->next=NULL; } Linklist reverse1(Linklist head)
{
//if linklist is a node or null ,we need not inverse the linklist
if(head==NULL||head->next==NULL)
{
return head;
} Linklist pre=head;
Node *current=head->next;
Linklist next1=current->next;
//notice that head->next should be null;
head->next=NULL; while(next1!=NULL)
{
current->next=pre;
pre=current; current=next1; next1=next1->next; } current->next=pre; return current; } void main()
{
cout<<"你好"<<endl;
Linklist h=NULL;
h=(Node *)malloc(sizeof(Node));
h->data=;
h->next=NULL; addNode(h,);
addNode(h,);
addNode(h,);
cout<<"反转前"<<endl;
print(h);
h=reverse1(h);
print(h);
cout<<"revese again"<<endl;
recursive2(h,h);
print(h); }

链表反转C实现(递归与循环)的更多相关文章

  1. java_链表反转

    定义一个Node节点类 1 public class Node { 2 public int value; 3 public Node next; 4 5 public Node(int value) ...

  2. 经典算法(三) 单链表 反转 & 是否相交/成环 & 求交点 等

    参考文章: 判断链表是否相交:http://treemanfm.iteye.com/blog/2044196 一.单链表反转 链表节点 public class Node { private int ...

  3. LeetCode 206——链表反转(JAVA)

    题目: 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶:你可 ...

  4. 单链表反转的原理和python代码实现

    链表是一种基础的数据结构,也是算法学习的重中之重.其中单链表反转是一个经常会被考察到的知识点. 单链表反转是将一个给定顺序的单链表通过算法转为逆序排列,尽管听起来很简单,但要通过算法实现也并不是非常容 ...

  5. 剑指offer—单链表反转的三种实现方法

    单链表的反转可以用递归.非递归和栈的方法实现 链表节点定义: struct ListNode{ int val; Node* next; ListNode(int x):val(x),next(nul ...

  6. Java单链表反转图文详解

    Java单链表反转图文详解 最近在回顾链表反转问题中,突然有一些新的发现和收获,特此整理一下,与大家分享 背景回顾 单链表的存储结构如图: 数据域存放数据元素,指针域存放后继结点地址 我们以一条 N1 ...

  7. 2、java数据结构和算法:单链表: 反转,逆序打印, 合并二个有序链表,获取倒数第n个节点, 链表的有序插入

    什么也不说, 直接上代码: 功能点有: 1, 获取尾结点 2, 添加(添加节点到链表的最后面) 3, 添加(根据节点的no(排名)的大小, 有序添加) 4, 单向链表的 遍历 5, 链表的长度 6, ...

  8. 链表反转leetcode206

    最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的 ...

  9. 链表反转 (Multi-method)

    链表反转是链表相关问题最基础的知识,做完LeetCode中LinkedList后才会有这种体会,因为ACM算法中不会涉及这一部分.解决这一问题有多种方法,在面试中面试官通常也会要求写出多种.包括sta ...

  10. java实现单链表反转

    一.简介 经查阅,主要有两种方法实现链表反转,递归反转法和遍历反转法: 递归: 在反转当前结点之前先反转其后边的结点,即.从尾结点开始逆向反转各个节点的指针域指向: 遍历:从前往后反转各个结点的指针域 ...

随机推荐

  1. 发光的input框(纯css实现)

    css代码: input{width: 200px;height: 40px;} input.focus{border-color: #08c;box-shadow: 0 0 4px #8bd6fb; ...

  2. Python3.4 多线程

    线程安全和全局解释器锁 Thread State and the Global Interpreter Lock 总结: 通过使用GIL后, Python多线程安全, 并且数据保持同步. Python ...

  3. 解决关于IIS10.0下无法安装 URL 重写模块 2的问题

    win10 系统自带的IIS是IIS10.0,官网提示URL Rewrite 2.0是只要IIS7.0以上的版本就可以安装,但是在IIS10.0下安装却一直失败.错误提示如下: 那么如何才能正确安装呢 ...

  4. MaskedTextBox控件实现输入验证

    Mask属性可以验证用户在文本中输入数据的格式 this.maskedTextBox1.Mask = "000000-00000000-000A";//身份证号码18位 this. ...

  5. Xcode 7.3.1的模拟器路径

    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Core ...

  6. asp.net推送

    http://tech.it168.com/a2012/0210/1310/000001310252_all.shtml http://www.infoq.com/cn/news/2012/09/rc ...

  7. MySQL db优化

    http://blog.csdn.net/likika2012/article/details/38816037 http://www.nowamagic.net/librarys/veda/deta ...

  8. cocos-html5 Json 灵活 遍历方式 不同方式的缺陷,优点总结

    1,四种解析Json的方式:Part 1 var list1 = [1,3,4]; alert(list1[1]); var list2 = [{"name":"leam ...

  9. *[topcoder]HexagonalBoard

    http://community.topcoder.com/stat?c=problem_statement&pm=12784 真心觉得tc的div1 250不少好题,对我来说比较适合.这道题 ...

  10. eCos中断模型

    http://blog.csdn.net/chychc/article/details/8313458 http://www.cnblogs.com/RandyQ/archive/2013/04/14 ...