#206. Reverse Linked List
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
这道题是把一个链表反转。
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
if(!head){
return head;
}
let new_head = head;
let current = null;
while(head.next) {
current = head.next
head.next = head.next.next
current.next = new_head
new_head = current
}
return new_head
};