Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

86. 分隔链表 #48

Open
buuing opened this issue Jun 6, 2021 · 0 comments
Open

86. 分隔链表 #48

buuing opened this issue Jun 6, 2021 · 0 comments

Comments

@buuing
Copy link
Owner

buuing commented Jun 6, 2021

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

示例 1:

image

输入:head = [1,4,3,2,5,2], x = 3
输出:[1,2,2,4,3,5]

示例 2:

输入:head = [2,1], x = 2
输出:[1,2]

提示:

  • 链表中节点的数目在范围 [0, 200] 内
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/partition-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。




const partition = (head, x) => {
  let minHead = currMin = { val: -Infinity }
  let prev = curr = { next: head }
  while (curr.next) {
    if (curr.next.val < x) {
      currMin.next = curr.next
      currMin = currMin.next
      curr.next = curr.next.next
    } else {
      curr = curr.next
    }
  }
  currMin.next = prev.next
  return minHead.next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant