# 反转链表
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
# 链表结构
function ListNode(val) {
this.val = val
// 指向下一个ListNode
this.next = null
}
# 代码实现
var reverseList = function (head) {
let parent = null
let current = head
while (current) {
const next = current.next
current.next = parent
parent = current
current = next
}
return parent
}
function ListNode(val) {
this.val = val
this.next = null
}
const a = new ListNode(1)
a.next = new ListNode(2)
a.next.next = new ListNode(3)
console.log(JSON.stringify(a, null, 2))
console.log(JSON.stringify(reverseList(a), null, 2))