【leetcode】反转链表

【leetcode】反转链表_第1张图片

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function(head) {
    if(head==null) return head;
    let stack=[];
    let point=head;
    while(point!=null){
        stack.push(point);
        point=point.next;
    }
    head=point=stack.pop();
    while(stack.length>0){
        point.next=stack.pop();
        point=point.next;
    }
    point.next=null;
    return head;
};

你可能感兴趣的:(力扣,算法,leetcode,链表,算法)