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->2->5->2, x = 3
Output: 1->2->2->4->3->5

题意

把链表按前面的全是小于x的,后面的全是大于等于x的顺序重组

题解

 1 class Solution {
 2 public:
 3     ListNode* partition(ListNode* head, int x) {
 4         ListNode*less = new ListNode(0),*lessh=less, *greater = new ListNode(0),*greaterh=greater;
 5         while (head) {
 6             ListNode*after = head->next;
 7             if (head->val < x) {
 8                 less->next = head;
 9                 less = less->next;
10                 less->next = NULL;
11             }
12             else {
13                 greater->next = head;
14                 greater = greater->next;
15                 greater->next = NULL;
16             }
17             head = after;
18         }
19         less->next = greaterh->next;
20         return lessh->next;
21     }
22 };

我就总爱各种地方有漏洞……面的时候也差不多……惨

原文地址:https://www.cnblogs.com/yalphait/p/10421638.html

时间: 2024-10-05 05:57:48

19.2.23 [LeetCode 86] Partition List的相关文章

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] 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划分链表

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->

19.2.23 [LeetCode 85] Maximal Rectangle

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

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 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-&

[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. Example: Input: head = 1->4->3-

19.2.23 [LeetCode 91] Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode it. Example 1: Input: &qu