### title

Example:

``````Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL``````

A linked list can be reversed either iteratively or recursively. Could you implement both?

### Analyze

``````      prev  cur  next
1 -> 2 -> 3 -> 4 -> 5 -> null
null <- 1 -> 2 -> 3 -> 4 -> 5``````

step:

1. 定义三个变量 prev, cur, next 表示上一个值, 当前值, 下一个值;
2. 如果存在 cur.next 则将 cur.next 指向 prev;
3. 将 cur 移动到 next 位置, prev 移动到 cur 位置, 重复步骤 2;
``````/**
* function ListNode(val) {
*     this.val = val;
*     this.next = null;
* }
*/
/**
* @return {ListNode}
*/
let prev = null
while (cur.next) {
cur.next = prev
prev = cur
cur = next
next = next.next
}
cur.next = prev
}
return cur
};``````

``````/**
* @return {ListNode}
*/
let prev = null
while (cur !== null) {
let next = cur.next
cur.next = prev
prev = cur
cur = next
}
return prev
};``````

``````/**
* @return {ListNode}
*/
let prev = null

const recursiveFn = () => {
if (cur === null) return

let next = cur.next
cur.next = prev
prev = cur
cur = next

recursiveFn()
}
recursiveFn()
return prev
};``````

92