SOURCE

class ListNode {
    constructor(data) {
        this.data = data
        this.next = null
    }
}

class LinkedList {
    constructor() {
        this.head = null
        this.tail = this.head
        this.length = 0
    }

    append(value) {
        const newNode = new ListNode(value)

        if (!this.head) {
            this.head = newNode
            this.tail = newNode
        } else {
            this.head.next = newNode
            this.tail = newNode
        }

        this.length++
    }

    reverse() {
        let preNode = null
        let curNode = this.head

        while(curNode !== null) {
            let nextNode = curNode.next

            curNode.next = preNode
            preNode = curNode
            curNode = nextNode
        }

        this.head = preNode
    }
}

console 命令行工具 X clear

                    
>
console