链表笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package  test;
public class ListNode{
    int val;
    ListNode next;
    public ListNode(int x) {
        val = x;
    }
     
    public ListNode() {}
     
     
    public static void main(String[] args) {
        int n = 2;
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        ListNode head = l1;
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        while(head != null) {
            System.out.println(head.val);
            head = head.next;
        }
         
        ListNode h1 = new ListNode();
        h1.next = head; //这里将 h1 指向了 head<br>          ListNode first = h1; // 对 first 操作的时候, h1 和 head 也在改变。在内存三者共用同一块内存
        first.next = l3;
         
        while(h1 !=  null) {
            System.out.println(h1.val);
            h1 = h1.next;
        }
        while(head != null) {
            System.out.println(head.val);
            head = head.next;
        }
         
    }
     
}
posted @   stul  阅读(123)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示