import java.lang.*; import java.util.*; import node.*; class ReverseList { public static ListNode reverseList(ListNode head) { ListNode prev = null; ListNode node = head; while (node != null) { ListNode tmp = node.next; node.next = prev; prev = node; node = tmp; } return prev; } public static void main(String[] args) { ListNode fourth = new ListNode(5, null); ListNode third = new ListNode(4, fourth); ListNode second = new ListNode(3, third); ListNode first = new ListNode(2, second); ListNode head = new ListNode(1, first); ListNode curr = reverseList(head); while (curr != null) { System.out.println(curr.val); curr = curr.next; } } }