数据结构——Java实现单向链表

结点类:

 1 /**
 2  * @author zhengbinMac
 3  * 一个OnelinkNode类的对象只表示链表中的一个结点,通过成员变量next的自引用方式实现线性表中各数据元素的逻辑关系。
 4  */
 5 public class OnelinkNode {
 6     // 保存结点的值
 7     public int data;
 8     // 保存后继结点的引用
 9     public OnelinkNode next;
10     // 构造值为k的结点
11     public OnelinkNode(int k) {
12         data = k;
13         next = null;
14     }
15     // 初始化单向链表
16     public OnelinkNode() {
17         this(0);
18     }
19 }

单向链表类:

 1 /**
 2  * @author zhengbinMac
 3  * Onelink1类的一个对象表示一条单向链表,成员变量head作为链表的头指针,指向
 4  * 链表的第一个结点。head为被保护的(protected),可被其子类继承。
 5  * 当head为null时,表示链表为空,元素个数为0。
 6  */
 7 public class Onelink1 {
 8     // 指向链表的第一个结点
 9     protected OnelinkNode head;
10     // 创建一个空的单向链表
11     public Onelink1() {
12         head = null;
13     }
14     // 构造由h1指向的单向链表
15     public Onelink1(OnelinkNode h1) {
16         head = h1;
17     }
18     /**
19      * 判断链表是否为空
20      */
21     public boolean isEmpty() {
22         return head == null;
23     }
24     /**
25      * 以n个随机值建立单向链表
26      */
27     public Onelink1(int n) {
28         OnelinkNode rear,q;
29         if(n > 0) {
30             int k = (int)(Math.random() * 100);//产生随机数,加入链表
31             head = new OnelinkNode(k);
32             rear = head;
33             for(int i = 1;i < n;i++) {
34                 k = (int)(Math.random() * 100);
35                 q = new OnelinkNode(k);
36                 rear.next = q;
37                 rear = q;
38             }
39         }
40     }
41     /**
42      * 返回链表的长度
43      */
44     public int length() {
45         int n = 0;
46         OnelinkNode p = head;
47         while(p != null) {
48             n++;
49             p = p.next;
50         }
51         return n;
52     }
53     /**
54      * 输出链表
55      */
56     public void output() {
57         this.output(head);
58     }
59     private void output(OnelinkNode head) {
60         System.out.print(this.getClass().getName() + ": ");
61         while(head != null) {
62             System.out.print(head.data);
63             head = head.next;
64             if(head != null) {
65                 System.out.print(" -> ");
66             }
67         }
68         System.out.println();
69     }
70 }

单向链表的反转:

 1 /**
 2  * @author zhengbinMac
 3  */
 4 public class Onelink2 extends Onelink1{
 5     public Onelink2() {
 6         super();
 7     }
 8     public Onelink2(int n) {
 9         super(n);
10     }
11     public void reverse() {
12         OnelinkNode p = this.head, q = null, front = null;
13         /*
14          * 以下面这个链表为例:
15          *  1->2->3->4
16          */
17         while(p != null) {
18             q = p.next;        // p = 1, q = 2;
19             p.next = front;   // 1.next = null;下次循环将变为:2.next = 1;
20             front = p;        // front = 1;
21             p = q;            // p = 2;
22         }
23         this.head = front;// 循环结束后,front指向原链表的最后一个结点,
24     }
25     public static void main(String[] args) {
26         Onelink2 h2 = new Onelink2(5);
27         h2.output();
28         System.out.println("Reverse!");
29         h2.reverse();
30         h2.output();
31     }
32 }

在线编程:

牛客网——《剑指Offer》-反转链表

原文地址:https://www.cnblogs.com/zhengbin/p/5665253.html