反转单链表和双向链表

 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 }

 

转载于:https://www.cnblogs.com/lihuazhu/p/10842645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值