#56 Reversing a Singly Linked List
Write a revert() method that accepts a singly linked list and returns a new list whose elements are in reverse order. Write a printList() method that prints the list.
public class Main {
public static void main(String[] args) {
Node first = new Node();
first.value = 1;
Node second = new Node();
second.value = 2;
first.next = second;
Node third = new Node();
third.value = 3;
second.next = third;
printList(first);
Node output = revert(first);
printList(output);
}
public static Node revert(Node input) {
...
}
public static void printList(Node input) {
...
}
}
class Node {
Node next;
int value;
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor