public class singleList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
value = data;
}
}
public static Node reverseList(Node head) {
Node pre = null;
Node next;
while(Objects.nonNull(head)) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
public static void main(String[] args) {
Node node = new Node(1);
node.next = new Node(2);
node.next.next = new Node(3);
node.next.next.next = new Node(4);
node.next.next.next.next = new Node(5);
Node reverseNode = reverseList(node);
while (reverseNode != null) {
System.out.print(reverseNode.value + " ");
reverseNode = reverseNode.next;
}
System.out.println();
}
}