xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 反转链表算法题解 All In One

LeetCode 反转链表算法题解 All In One

js / ts 实现反转链表

反转链表原理 图解

双指针,swap 交换

  // 反转 双指针
  // swap: a = b; c = a; b = c;
  let prev: ListNode | null = null;
  let cur: ListNode | null = head;
  // while(cur) {
  //   // ES5 swap 缓存引用类型,防止修改后丢失数据 👎
  //   let temp = cur.next;
  //   cur.next = prev;
  //   prev = cur;
  //   cur = temp;
  // }
    while(cur) {
    // ES6 swap 不需要缓存引用类型 🚀
    [
     cur.next,
     prev,
     cur,
    ] = [
     prev,
     cur,
     cur.next,
    ];
  }
const head = {
  val: 1,
  next: {
    val: 2,
    next: {
      val: 3,
      next: {
        val: 4,
        next: {
            val: 5,
            next: null,
        }
      }
    }
  }
};

206. Reverse Linked List

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-08-15
 * @modified
 *
 * @description 206. Reverse Linked List
 * @description 206. 反向链表
 * @difficulty Easy
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/reverse-linked-list/
 * @link https://leetcode.cn/problems/reverse-linked-list/
 * @solutions
 *
 * @best_solutions
 *
 */

export {};

const log = console.log;

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

class ListNode {
  val: number
  next: ListNode | null
  constructor(val?: number, next?: ListNode | null) {
    this.val = (val===undefined ? 0 : val)
    this.next = (next===undefined ? null : next)
  }
}


function reverseList(head: ListNode | null): ListNode | null {
  if(!head || !head.next) {
     return head;
  }
  // 反转 双指针
  // swap: a = b; c = a; b = c;
  let prev: ListNode | null = null;
  let cur: ListNode | null = head;
  // while(cur) {
  //   // ES5 swap 缓存引用类型,防止修改后丢失数据 👎
  //   let temp = cur.next;
  //   cur.next = prev;
  //   prev = cur;
  //   cur = temp;
  // }
    while(cur) {
    // ES6 swap 不需要缓存引用类型 🚀
    [
     cur.next,
     prev,
     cur,
    ] = [
     prev,
     cur,
     cur.next,
    ];
  }
  // console.log(`prev =`, JSON.stringify(prev));
  // console.log(`prev =`, JSON.stringify(prev, null, 2));
  return prev;
};



const head = {
  val: 1,
  next: {
    val: 2,
    next: {
      val: 3,
      next: {
        val: 4,
        next: {
          val: 5,
          next: null,
        }
      }
    }
  }
};


/*

console.log(`head =`, JSON.stringify(head));
// console.log(`head =`, JSON.stringify(head, null, 2));

reverseList(head);

*/



// 测试用例 test cases
const testCases = [
  {
    input: head,
    result: `{"val":5,"next":{"val":4,"next":{"val":3,"next":{"val":2,"next":{"val":1,"next":null}}}}}`,
    desc: 'value equal to {"val":5,"next":{"val":4,"next":{"val":3,"next":{"val":2,"next":{"val":1,"next":null}}}}}',
  },
];

for (const [i, testCase] of testCases.entries()) {
  const result = reverseList(testCase.input);
  log(`test case i result: \n`, JSON.stringify(result) === testCase.result ? `passed ✅` : `failed ❌`, result);
  // log(`test case i =`, testCase);
}

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-11-17
 * @modified
 *
 * @description 206 reverse-linked-list
 * @description 206 反转链表
 * @difficulty Easy
 * @complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/reverse-linked-list/
 * @link https://leetcode-cn.com/problems/reverse-linked-list/
 * @solutions
 *
 */

const log = console.log;


var reverseList = function(head) {
  let [
    prev,
    curr
  ] = [
    null,
    head
  ];
  while (curr) {
    // swap
    [
      curr.next,
      prev,
      curr
    ] = [
      prev,
      curr,
      curr.next
    ];
  }
  return prev;
};



/*

var reverseList = function(head) {
  // let prev = null;
  // let curr = head;
  let [prev, curr] = [null, head];
  while (curr) {
    // let temp = curr.next;
    // curr.next = prev;
    // prev = curr;
    // curr = temp;
    [curr.next, prev, curr] = [prev, curr, curr.next];
  }
  return prev;
};

*/


/**
 * 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) {
    return head;
  }
  // 链尾
  // let end = new ListNode(head.val);
  let end = null;
  // 链首
  let temp = head;
  // 交换
  while(temp) {
    // 临时的子链表 ✅
    const next = temp.next;
    // 反转 next
    temp.next = end;
    // 新链尾
    end = temp;
    // 迭代持续进行的条件
    temp = next;
  }
  return end;
};


https://leetcode.com/problems/reverse-linked-list/
https://leetcode.cn/problems/reverse-linked-list/

https://leetcode.cn/problems/fan-zhuan-lian-biao-lcof/

leetcode 题解 / LeetCode Solutions

https://www.youtube.com/results?search_query=+Leetcode+2066

https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE

https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos

https://neetcode.io/

https://github.com/neetcode-gh/leetcode/blob/main/javascript/206-Reverse-Linked-List.js

https://github.com/neetcode-gh/leetcode/blob/main/typescript/206-Reverse-Linked-List.ts

类似问题

LeetCode

refs

https://www.cnblogs.com/xgqfrms/tag/反转链表/



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-08-15 23:30  xgqfrms  阅读(24)  评论(2编辑  收藏  举报