用JavaScript刷leetcode第86题-分隔链表

1,286 阅读4分钟

前言

一开始觉得这题贼简单。真的是Talk is easy, show me your code, 我写的代码提交leetcode 时, 报了如下图的错,意思是说成环了。 image.png 我思索了会,是不是我处理每个节点的之前,没有把每个节点的next 属性置于 null, 导致有环。问题是解决了。
但是我看题解的时候,并不是要处理每个节点的next属性。只需要拼接前,处理大于x链表的尾节点即可。

一、题目描述:

  • 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
  • 你应当 保留 两个分区中每个节点的初始相对位置。
  • 示例:
输入: head = [1,4,3,2,5,2], x = 3
输出:[1,2,2,4,3,5]

详细描述:请看leetcode题目描述

二、解题:

2.1 思路:

  • 遍历原链表
  • 拆成两个链表,链表一的所有节点值小于x , 链表二的所有节点值大于等于x
  • 再连接这两个链表,链表一的尾接链表二的头, 连接后的链表可能成环吗?
  • 返回链表一的头

2.2 代码

git代码地址

2.2.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
 * @param {number} x
 * @return {ListNode}
 */
 
const partition = function(head, x) {
  // small链表的节点值 都 小于x, smallHead 是其虚头
  let small = new ListNode(-1), smallHead = small
  // large链表的节点值 都 大于等于 x, largeHead 是其虚头
  let large = new ListNode(-1), largeHead = large

  // 遍历原链表,分隔成两个链表
  while(head) {
    if(head.val < x) {
      // 小于x的节点 放到 small 链表尾, small链表尾指针 往后移动一位
      small.next = head
      small = small.next
    } else {
      // 大于等于x的节点 放到 large 链表尾, large链表尾指针 往后移动一位
      large.next = head
      large = large.next
    }

    // 开始原链表下次循环
    head = head.next
  }

  // small链表 拼接 large链表(注意:samll尾 拼 large真实头)
  small.next = largeHead.next

  // 返回small真实头
  return smallHead.next
};

2.2.2 正确代码:好理解

遍历原链表分隔成两个链表时,先断开待处理节点的next,再分隔。 其实就是在whiLe循环里加了两行代码和改了一行代码。
改动伪代码

while(head) {
  // 开始原链表下次循环
  const next = head.next
  head.next = null
  
  ...
 
  // 开始原链表下次循环
  // head = head.next
  head = next
}

完整代码

const partition = function(head, x) {
  // small链表的节点值 都 小于x, smallHead 是其虚头
  let small = new ListNode(-1), smallHead = small
  // large链表的节点值 都 大于等于 x, largeHead 是其虚头
  let large = new ListNode(-1), largeHead = large

  // 遍历原链表,分隔成两个链表
  while(head) {
    // 断开当前节点的next
    const next = head.next
    head.next = null
    
    if(head.val < x) {
      // 小于x的节点 放到 small 链表尾, small链表尾指针 往后移动一位
      small.next = head
      small = small.next
    } else {
      // 大于等于x的节点 放到 large 链表尾, large链表尾指针 往后移动一位
      large.next = head
      large = large.next
    }

    // 开始原链表下次循环
    // head = head.next
    head = next
  }

  // small链表 拼接 large链表(注意:samll尾 拼 large真实头)
  small.next = largeHead.next

  // 返回small真实头
  return smallHead.next
};

2.2.3 正确代码:细节处理更好

问大家一个问题什么时候会成环?
large链表的尾节点next不断开处理的话,可能会指向small链表中的某个节点,这种情况拼接后会成环。
故我们只需要在拼接前处理即可,在错误代码的基础上加一行代码large.next = null
代码:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} x
 * @return {ListNode}
 */
 
const partition = function(head, x) {
  // small链表的节点值 都 小于x, smallHead 是其虚头
  let small = new ListNode(-1), smallHead = small
  // large链表的节点值 都 大于等于 x, largeHead 是其虚头
  let large = new ListNode(-1), largeHead = large

  // 遍历原链表,分隔成两个链表
  while(head) {
    if(head.val < x) {
      // 小于x的节点 放到 small 链表尾, small链表尾指针 往后移动一位
      small.next = head
      small = small.next
    } else {
      // 大于等于x的节点 放到 large 链表尾, large链表尾指针 往后移动一位
      large.next = head
      large = large.next
    }

    // 开始原链表下次循环
    head = head.next
  }
  
  // 可能成环的处理
  large.next = null

  // small链表 拼接 large链表(注意:samll尾 拼 large真实头)
  small.next = largeHead.next

  // 返回small真实头
  return smallHead.next
};