public class Node
{
private Node next;
private String value;
/**
*
*/
public Node(String value)
{
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实现链表的反转
最新推荐文章于 2024-11-02 16:53:29 发布