leetcode日常刷题第三题--翻转链表

leetcode日常刷题第三题--翻转链表

这道题 用到了双指针法 比较简单 没什么好说的 就是指针的交换使用需要好好思考思考 我觉得这道题的精妙之处就是那个swap中间值,后续的迭代算法和其他方法 我学习之后会写一写

/**
 * 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 || !head.next) return head;
    let pre=null
    let swap=null
    let cur=head
    while(cur){
        swap=cur.next
        cur.next=pre
        pre=cur
        cur=swap
    }
    return pre
};

LICENSED UNDER CC BY-NC-SA 4.0