Java ---- 链表逆序

public class LinkedListRevert {
public static void main(String[] args) { Node next3 = new Node(4,null);
Node next2 = new Node(3,next3);
Node next = new Node(2,next2);
Node head =new Node(1,next); Node tmp = head; // 保存首节点
while (head!=null){ System.out.println(head.value);
head=head.next;
}
Node revertHead = revert(tmp); while (revertHead!=null){ System.out.println(revertHead.value);
revertHead=revertHead.next;
}
} public static Node revert(Node head){ Node prev = null;
Node next = head.next; while(head!=null){
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
} class Node{
int value;
Node next;
public Node (int value,Node next){
this.value = value;
this.next = next;
}
}
上一篇:分享java常用技术教程:dubbo、zookeeper、ActiveMq、多线程、Redis、PowerDesigner等


下一篇:表单属性问题readonly、disabled、checked,prop的使用