zoukankan      html  css  js  c++  java
  • 单链表反转

    我写的单链表反转  欢迎大家提意见   菜鸟一个

    head表示当前的  pre是head前一个   next是head后一个

    /*
    * 单链表逆转
    */
    class Node {
    int value;
    Node next;

    public Node(int N_value) {
    this.value = N_value;
    }

    public Node(int N_value, Node next) {
    this.value = N_value;
    this.next = next;
    }

    }

    public class ListReverse {
    private static void PrintList(Node head) {
    while (head != null) {
    System.out.print(head.value + " ");
    head = head.next;
    }
    }

    public static void main(String[] args) {
    Node n5 = new Node(5);
    Node n4 = new Node(4, n5);
    Node n3 = new Node(3, n4);
    Node n2 = new Node(2, n3);
    Node n1 = new Node(1, n2);
    PrintList(n1);
    Node NewHead = Revert2(n1);
    System.out.println("递归调用");
    PrintList(NewHead);

    System.out.println("非递归调用");
    PrintList(Revert(NewHead));
    }

    /*
    * 非递归调用
    */
    private static Node Revert(Node n1) {
    if (n1 != null) {
    Node pre = null;
    Node head = n1;
    Node next = n1.next;
    while (next != null) {
    head.next = pre;
    pre = head;
    head = next;
    next = next.next;
    }
    head.next = pre;
    return head;

    //递归调用
    }else {
    return null;
    }
    }

    /*
    * 递归调用
    */
    private static Node Revert2(Node current) {
    //current!=null 是拒绝空链表判断用的

    if (current == null || current.next == null){
    return current;
    }
    Node nextNode = current.next;
    current.next = null;
    Node reverseRest = Revert2(nextNode);
    nextNode.next = current;
    return reverseRest; //其实这里面每次return的节点都是一样的
    }

    }

    
    
    
    
    
    

     http://blog.csdn.net/beiyetengqing/article/details/7596554

    http://poly.iteye.com/blog/1748272

    感觉上面两个写的比我的好,学习一下

  • 相关阅读:
    ps插件安装
    CSS3时钟式进度条
    手机web——自适应网页设计(html/css控制)
    7个设计师必备的国际顶尖设计网站
    中​文​字​号​、​磅​和​像​素​对​照​关​系
    图标字体
    用AE如何制作如下三个loading动效,
    u盘装系统
    SpringBoot:Maven创建一个HelloWorld
    eclipse中配置maven和创建第一个 Spring Boot Application
  • 原文地址:https://www.cnblogs.com/chengpeng15/p/5805228.html
Copyright © 2011-2022 走看看