一、新建链表
class Link{
private Node root;
class Node{
private String name;
private Node Next;
public Node(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void addNode(Node newNode){
if(this.Next==null){
this.Next = newNode;
}else{
this.Next.addNode(newNode);
}
}
public void printNode(){
System.out.print(this.name + "-->");
if(this.Next!=null){
this.Next.printNode();
}
}
};
public void add(String name){
Node newNode = new Node(name);
if(this.root==null){
this.root = newNode;
}else{
this.root.addNode(newNode);
}
}
public void print(){
if(this.root!=null){
this.root.printNode();
}
}
};
public class LinkDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Link link = new Link();
link.add("根节点");
link.add("第一节点");
link.add("第二节点");
link.add("第三节点");
link.add("第四节点");
link.print();
System.out.println("null");
}
}
链接:http://blog.csdn.net/chichoxian/article/details/8549744
二、
输入一个链表,从尾到头打印链表每个节点的值。
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<>();
while (listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> list = new ArrayList<>();
while (!stack.isEmpty()) {
list.add(stack.pop());
}
return list;
}
}