[leetcode]86. Partition List划分链表

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

题意:

给定一个链表和一个值,把小于等于和大于该值的部分分别放到链表的一前一后去。

思路:

先分为两个链表

然后合并

代码:

 1 class Solution {
 2     public ListNode partition(ListNode head, int x) {
 3         ListNode leftDummy = new ListNode(-1);
 4         ListNode rightDummy = new ListNode (-1);
 5         ListNode left_cur = leftDummy;
 6         ListNode right_cur = rightDummy;
 7         ListNode cur = head;
 8
 9         while( cur != null){
10             if(cur.val < x){
11                 left_cur.next = cur;
12                 left_cur = cur;
13             }else{
14                 right_cur.next = cur;
15                 right_cur = cur;
16             }
17             cur = cur.next;
18         }
19        left_cur.next = rightDummy.next;
20        right_cur.next = null;
21        return leftDummy.next;
22     }

原文地址:https://www.cnblogs.com/liuliu5151/p/9227167.html

时间: 2024-11-05 17:20:33

[leetcode]86. Partition List划分链表的相关文章

[LeetCode] 86. Partition List 划分链表

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

[LeetCode]86. Partition List分离链表

/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(ListNode head, int x) { ListNode s = null; ListNode b = null; ListNode temp=null,res=null; while (head!=null) { int cur = head.val; if (head.val<x) { i

leetCode 86.Partition List(分区链表) 解题思路和方法

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3->

LeetCode --- 86. Partition List

题目链接:Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,

[LeetCode] Partition List 划分链表

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2

[LeetCode] 86. Partition List Java

题目: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3-&

19.2.23 [LeetCode 86] Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

[LeetCode] 86. Partition List 解题思路

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2

[LeetCode] 86. Partition List_Medium tag: Linked List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-