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

     1 package my_basic.class_3;
     2 
     3 public class Code_07_ReverseList {
     4     public static class Node{
     5         private int value;
     6         private Node next;
     7         
     8         public Node(int value) {
     9             super();
    10             this.value = value;
    11         }
    12     }
    13     
    14     /*反转单向链表*/
    15     public static Node reverseList(Node head) {
    16         Node next = null;
    17         Node pre = null;
    18         while (head != null) {
    19             next = head.next;
    20             head.next = pre;
    21             pre = head;
    22             head = next;
    23         }
    24         return pre;
    25     }
    26     
    27     public static class DoubleNode{
    28         private int value;
    29         private DoubleNode next;
    30         private DoubleNode last;
    31         public DoubleNode(int value) {
    32             super();
    33             this.value = value;
    34         }
    35     }
    36     
    37     public static DoubleNode reverseList(DoubleNode head) {
    38         DoubleNode next = null;
    39         DoubleNode pre = null;
    40         while (head != null) {
    41             next =head.next;
    42             head.next = pre;
    43             head.last = next;
    44             pre = head;
    45             head = next;
    46         }
    47         return pre;
    48     }
    49     
    50     public static void printLinkedList(Node head) {
    51         System.out.println("单链表:");
    52         while(head != null) {
    53             System.out.print(head.value + " ");
    54             head = head.next;
    55         }
    56         System.out.println();
    57     }
    58     
    59     public static void printDoubleLinkedList(DoubleNode head) {
    60         System.out.println("双向单链表:");
    61         DoubleNode end = null;
    62         while (head != null) {
    63             System.out.print(head.value + " ");
    64             end = head;
    65             head = head.next;
    66         }
    67         System.out.println("|");
    68         while (end != null) {
    69             System.out.print(end.value + " ");
    70             end = end.last;
    71         }
    72         System.out.println();
    73         
    74     }
    75     
    76     public static void main(String[] args) {
    77         Node head1 = new Node(1);
    78         head1.next = new Node(2);
    79         head1.next.next = new Node(3);
    80         printLinkedList(head1);
    81         head1 = reverseList(head1);
    82         printLinkedList(head1);
    83 
    84         DoubleNode head2 = new DoubleNode(1);
    85         head2.next = new DoubleNode(2);
    86         head2.next.last = head2;
    87         head2.next.next = new DoubleNode(3);
    88         head2.next.next.last = head2.next;
    89         head2.next.next.next = new DoubleNode(4);
    90         head2.next.next.next.last = head2.next.next;
    91         printDoubleLinkedList(head2);
    92         printDoubleLinkedList(reverseList(head2));
    93 
    94     }
    95 
    96 }
  • 相关阅读:
    autocad.net 利用linq获取矩形框内的块参照
    autocad.net 只在图纸空间遍历块的方法
    autocad.net中判断当前被激活的空间
    计划搞一个程序来应对客户的修改标记问题
    条件编译解决AutoCAD多版本问题
    初学者往往不知道怎么获得断点,请看下面的链接应该可以解决你的问题!
    2014年3月9日正式入住博客园
    学习:SpringCloud(一)
    简单使用:SpringBoot整合Redis
    Redis 使用过程中遇到的具体问题
  • 原文地址:https://www.cnblogs.com/lihuazhu/p/10842645.html
Copyright © 2011-2022 走看看