86:分隔链表

This commit is contained in:
huangge1199 2021-06-07 10:25:59 +08:00
parent 6c8be326fa
commit 967e0d87f1
2 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,78 @@
//给你一个链表的头节点 head 和一个特定值 x 请你对链表进行分隔使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前
//
// 你应当 保留 两个分区中每个节点的初始相对位置
//
//
//
// 示例 1
//
//
//输入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
//
// Related Topics 链表 双指针
// 👍 410 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
//86:分隔链表
public class PartitionList{
public static void main(String[] args) {
//测试代码
Solution solution = new PartitionList().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode max = new ListNode(0);
ListNode min = new ListNode(0);
ListNode maxHead = max;
ListNode minHead = min;
while (head != null) {
if (head.val >= x) {
maxHead.next = head;
maxHead = maxHead.next;
} else {
minHead.next = head;
minHead = minHead.next;
}
head = head.next;
}
maxHead.next = null;
minHead.next = max.next;
return min.next;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,30 @@
<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ,请你对链表进行分隔,使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p>
<p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/partition.jpg" style="width: 662px; height: 222px;" />
<pre>
<strong>输入:</strong>head = [1,4,3,2,5,2], x = 3
<strong>输出</strong>[1,2,2,4,3,5]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>head = [2,1], x = 2
<strong>输出</strong>[1,2]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>链表中节点的数目在范围 <code>[0, 200]</code></li>
<li><code>-100 <= Node.val <= 100</code></li>
<li><code>-200 <= x <= 200</code></li>
</ul>
<div><div>Related Topics</div><div><li>链表</li><li>双指针</li></div></div>\n<div><li>👍 410</li><li>👎 0</li></div>