翻转链表 递归 与 非递归
// 非递归翻转链表
var reverseList = function(head) {
let pre =null
let cur = head
while(cur){
let next = head.next
cur.next = pre
pre = head
cur = next
}
return pre
}
// 递归翻转链表
var reverseList = function(head) {
if(!head){ // 链表为null的情况
return head
}
if(!head.next){ // 即,到达了最后一个节点
return head
}
let reverse = reverseList(head.next)
head.next.next = head
head.next = null
return reverse
}