class ListNode<T> {
    public data: T;
    public next: ListNode<T>;
    
    constructor(data: T) {
        this.data = data;
        this.next = null;
    }
}
interface ILinkedList<T> {
    push(data: T);
    reverse();
    log();
}
class LinkedList<T> implements ILinkedList<T> {
    private head: ListNode<T>;
    
    push(data: T) {
        const node = new ListNode<T>(data);
        node.next = this.head;
        this.head = node;
    }
    reverse() {
        if (!this.head) { return; }
        let curr = this.head;
        let prev = null;
        let next = null;
        while (curr) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        this.head = prev;
    }
    log() {
        let output = ""
        let p = this.head;
        while (p) {
            output += p.data + " ";
            p = p.next;
        }
        console.log(output);
    }
}

// driver code below

const list = new LinkedList<number>();

for (let i=3; i>0; i--) {
    list.push(i);
}

list.log();
list.reverse();
list.log();

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: