import java.util.ArrayList;
import java.util.List;
public class ListNode {
private Integer value;
private ListNode next;
public ListNode(Integer value, ListNode next) {
this.value = value;
this.next = next;
}
@Override
public String toString() {
ListNode cur = this;
List<Integer> l = new ArrayList<Integer>();
while (cur != null) {
l.add(cur.value);
cur = cur.next;
}
return l.toString();
}
public static void main(String[] args) {
ListNode linkNode = new ListNode(0, null);
ListNode linkNode1 = new ListNode(1, null);
ListNode linkNode2 = new ListNode(2, null);
ListNode linkNode3 = new ListNode(3, null);
ListNode linkNode4 = new ListNode(4, null);
linkNode.next = linkNode1;
linkNode1.next = linkNode2;
linkNode2.next = linkNode3;
linkNode3.next = linkNode4;
System.out.println(linkNode);
ListNode cur = linkNode.next;
ListNode pre = linkNode;
pre.next = null;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
System.out.println(pre);
}
}
java单链表反转
最新推荐文章于 2024-08-19 19:55:55 发布