#56 Reversing a Singly Linked List

Companies:
Usetech
Themes:
AlgorithmsPractice

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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in