Sobes.tech
Junior — Senior

Inversion of a singly linked list

livecode

Task condition

Given a singly linked list implemented through the Node structure, it is required to reverse the order of the list elements and return a pointer to the new head.

struct Node {
    int value;
    Node* nxt;

    Node() : value(0), nxt(nullptr) {}
    Node(int x) : value(x), nxt(nullptr) {}
    Node(int x, Node* nxt) : value(x), nxt(nxt) {}
};

Node* ReverseList(Node* root) {
    // implement list reversal
}

Example:

Input: 1 → 2 → 3 → nullptr
Output: 3 → 2 → 1 → nullptr