0
点赞
收藏
分享

微信扫一扫

#yyds干货盘点# LeetCode程序员面试金典:分割链表

题目:

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

你不需要 保留 每个分区中各节点的初始相对位置。

 

示例 1:

#yyds干货盘点# LeetCode程序员面试金典:分割链表_链表

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

示例 2:

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

代码实现:

class Solution {
public ListNode partition(ListNode head, int x) {
ListNode small = new ListNode(0);
ListNode smallHead = small;
ListNode large = new ListNode(0);
ListNode largeHead = large;
while (head != null) {
if (head.val < x) {
small.next = head;
small = small.next;
} else {
large.next = head;
large = large.next;
}
head = head.next;
}
large.next = null;
small.next = largeHead.next;
return smallHead.next;
}
}

举报

相关推荐

0 条评论