class ListNode {
int value = 0;
ListNode next = null;
public ListNode(int value) {
this.value = value;
}
}
public static ListNode reverse(ListNode head) {
ListNode newHead = null;
while(head != null) {
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
ListNode listNode = new ListNode(1);
listNode.next = new ListNode(2);
listNode.next.next = new ListNode(3);
listNode.next.next.next = new ListNode(4);
ListNode reverse = reverse(listNode);
System.out.println(reverse.value);
System.out.println(reverse.next.value);
System.out.println(reverse.next.next.value);
System.out.println(reverse.next.next.next.value);