quoniammm / happy-leetcode-javascript

leetcode题解
MIT License
1 stars 0 forks source link

206 Reverse Linked List #10

Open quoniammm opened 9 years ago

quoniammm commented 9 years ago
/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    var prev = null,
        next;
    if(head === null)
        return null;
    while(true){
        next = head.next; 
        head.next = prev; 
        prev = head;
        if(next !== null)
            head = next;
        else 
            break;
    }
    return head; 
};