单向链表的建立(尾部插入法)

class ListNode {
    ListNode next;
    int val;
    public ListNode(int x) {
        val = x;
    }
}
public class LinkList {
    private ListNode curr = new ListNode(-1);
    private ListNode head = curr;
    public void appendToTail(int d) {
        ListNode tail = new ListNode(d);
        curr.next = tail;
        curr = curr.next;
    }
    public void printAppendToTail() {
        while (head.next != null) {
            System.out.println(head.next.val);
            head = head.next;
        }
    }
    
    public static void main(String[] args) {
        LinkList llist = new LinkList();
        for (int i = 1; i < 10; i++) {
            llist.appendToTail(i);
        }
        llist.printAppendToTail();
    }
}
原文地址:https://www.cnblogs.com/lasclocker/p/4856233.html