美文网首页
206. Reverse Linked List

206. Reverse Linked List

作者: jluemmmm | 来源:发表于2020-08-30 19:48 被阅读0次
  • Runtime: 84 ms, faster than 52.12% O(n)
  • Memory Usage: 38.7 MB, less than 30.77% O(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) {
    let prev = null
    let cur = head
    while(cur) {
        let temp = cur.next
        cur.next = prev
        prev = cur
        cur = temp
    }
    return prev
};

相关文章

网友评论

      本文标题:206. Reverse Linked List

      本文链接:https://www.haomeiwen.com/subject/gewesktx.html