面试题5:从尾到头打印链表

public class Demo5 {

	static void printlist_recursive(LinkNode head){
		if(head != null)
		{
			if(head.next != null)
			{
				printlist_recursive(head.next);
			}
			System.out.print(head.value);
		}
	}
	public static void main(String[] args) {
		LinkList list=new LinkList();
		LinkNode node1=list.createNode(1);
		LinkNode node2=list.createNode(2);
		LinkNode node3=list.createNode(3);
		LinkNode node4=list.createNode(4);
		LinkNode node5=list.createNode(5);
		list.connectNode(node1, node2);
		list.connectNode(node2, node3);
		list.connectNode(node3, node4);
		list.connectNode(node4, node5);
		list.printlist(node1);
		printlist_recursive(node1);
	}
}
class LinkNode {
	public int value;
	public LinkNode next;
	public LinkNode() {
		super();
		// TODO Auto-generated constructor stub
	}
	public LinkNode(int value) {
		super();
		this.value = value;
	}
	public void display()      // display ourself
	{
	   System.out.print(value);
	}	
}
class LinkList
{
	LinkNode createNode(int value)
	{
		LinkNode node=new LinkNode(value);
		node.next=null;
		return node;
	}
	
	void connectNode(LinkNode current,LinkNode next)
	{
		if(current == null)
		{
			System.out.println("Error to connect two nodes.");
		}
		current.next=next;
	}
	void printlist(LinkNode head)
	{
		LinkNode node=head;
		while(node != null){
			System.out.print(node.value);
			node=node.next;
		}
		System.out.println("");
	}
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值