LeetCode 题解(159): Partition List

2024-05-28 09:08

本文主要是介绍LeetCode 题解(159): 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->5->2 and x = 3,
return 1->2->2->4->3->5.

题解:

Dummy头指针保证比x小,解决2->1, x = 2的问题。front指针记录第一个大于等于x的节点位置。挪动p, q指针。

C++版:

class Solution {
public:ListNode* partition(ListNode* head, int x) {if(head == NULL)return NULL;ListNode* dummy = new ListNode(x-1);dummy->next = head;ListNode* p = dummy;while(p->next && p->next->val < x)p = p->next;ListNode* q = p->next;ListNode* front = p->next;while(q) {if(q->next && q->next->val < x) {p->next = q->next;q->next = q->next->next;p->next->next = front;p = p->next;} else if(q->next && q->next->val >= x) {q = q->next;} elsebreak;}return dummy->next;}
};

Java版:

public class Solution {public ListNode partition(ListNode head, int x) {if(head == null)return null;ListNode dummy = new ListNode(x-1);dummy.next = head;ListNode p = dummy;while(p.next != null && p.next.val < x)p = p.next;ListNode q = p.next;ListNode front  = p.next;while(q != null) {if(q.next != null && q.next.val < x) {p.next = q.next;q.next = q.next.next;p.next.next = front;p = p.next;} else {q = q.next;}}return dummy.next;}
}

Python版:

class Solution:# @param {ListNode} head# @param {integer} x# @return {ListNode}def partition(self, head, x):if head == None:return Nonedummy = ListNode(x-1)dummy.next = headp = dummywhile p.next != None and p.next.val < x:p = p.nextq = p.nextstart = p.nextwhile q != None:if q.next != None and q.next.val < x:p.next = q.nextq.next = q.next.nextp.next.next = startp = p.nextelif q.next != None and q.next.val >= x:q = q.nextelse:breakhead = dummy.nextreturn head


这篇关于LeetCode 题解(159): Partition List的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010136

相关文章

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return