[TOC]

单链表

单链表创建

static Node SingleNode(int[] a) {
if (a.length < 1) {
return null;
}
Node head = new Node(a[0]);
Node cur = head;
for (int i = 1; i < a.length; i++) {
cur.next = new Node(a[i]);
cur = cur.next;
}
return head;
}

单链表逆序输出

static Node f(Node head) {
if (head == null) {
return head;
}
Node node=f(head.next);
System.out.print(head.data + " ");
return node;
}

反转单链表

链表为:1->2->3->4。反转后为 4->3->2->1

static Node f2(Node head) {
if (head == null||head.next==null) {
return head;
}
Node node=f2(head.next);
head.next.next=head;
head.next=null;
return node;
}
head=f2(head);

逆序输出并未反转

static Node f(Node head) {
if (head == null) {
return head;
}
Node node=f(head.next);
System.out.print(head.data + " ");
return node;
}
f(head);