论坛首页 Java企业应用论坛

java实现的链表的反转

浏览 3390 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (4)
作者 正文
   发表时间:2009-03-03  
OO

 

/**
 * 
 */
package com.test;

/**
 * @author Administrator
 * 
 */
public class Node {

	private Node next;
	private String value;

	/**
	 * 
	 */
	public Node(String value) {
		// TODO Auto-generated constructor stub
		this.value = value;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String toString() {
		return value;
	}

	public static void main(String[] args) {

		Node head = new Node("aa");
		Node node1 = new Node("bb");
		Node node2 = new Node("cc");
		Node node3 = new Node("dd");
		Node node4 = new Node("ee");
		head.setNext(node1);
		node1.setNext(node2);
		node2.setNext(node3);
		node3.setNext(node4);
		test(head);
		head.next = null;
		while (node4 != null) {
			System.out.print(node4);
			node4 = node4.next;
			if (node4 != null)
				System.out.print("->");
		}
	}

	private static void test(Node head) {
		if (head != null) {
			Node nNode = head.next;
			if (nNode != null) {
				test(nNode);
				nNode.next = head;
			}

		}

	}

}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics