【LeetCode】Partition List 链表划分
来源:互联网 发布:手机视频录播软件 编辑:程序博客网 时间:2024/06/09 20:26
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.
即:给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。
你应该保留两部分内链表节点原有的相对顺序。
样例
给定链表 1->4->3->2->5->2->null,并且 x=3
返回 1->2->2->4->3->5->null
标签
两根指针 链表
相关题目
中等 数组划分 30 %
(1)Java
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */public class Solution { /* * @param head: The first node of linked list * @param x: An integer * @return: A ListNode */ public ListNode partition(ListNode head, int x) { // write your code here if(head == null){ return null;//coding style!!! } ListNode leftDummy = new ListNode(0); ListNode rightDummy = new ListNode(0); ListNode left = leftDummy, right = rightDummy; while(head != null){ if(head.val < x){ left.next = head; left = head; }else{ right.next = head; right = head; } head = head.next; } right.next = null; left.next = rightDummy.next; return leftDummy.next; }}
(2)C++
#include "stdafx.h"#include <iostream>#include <cstdio>using namespace std;typedef struct tagSNode{ int val; tagSNode* pNext; tagSNode(int v) : val(v), pNext(NULL) {}}SNode;void Print(SNode* sn) { SNode* p; p = sn->pNext; while (p) { if (p->pNext != NULL) { cout << p->val << " -> "; } else { cout << p->val; } p = p->pNext; } cout << endl;}void Destroy(SNode* p) { SNode* next/* = p->pNext*/; while (p) { next = p->pNext; delete p; p = next;//此处不可以直接p = p->pNext (×)!!就算此句位于delete p之前,也错!(因为这样改变了指针p的位置!) //最好的方法:定义临时指针temp(is analogous to 'SNode* temp') }}void Partition(SNode* pHead, int pivotKey) {if (pHead == NULL) {//Coding style!!! return; } //两链表的头指针 SNode* pLeftpHead = new SNode(0); SNode* pRightHead = new SNode(0); //两链表的当前最后一个元素 SNode* left = pLeftpHead; SNode* right = pRightHead; SNode* p = pHead->pNext; while (p)//遍历原链表 { if (p->val < pivotKey) { left->pNext = p; left = p;//指针后移:等价于left = left->pNext; } else { right->pNext = p; right = p;//指针后移:等价于 right = right->pNext; } p = p->pNext; } //将right后置空; 将right部分(的头指针后面部分)连接到left的尾部 right->pNext = NULL; left->pNext = pRightHead->pNext;//不是right!保护表头结点! //将整理好的链表赋给当前链表头部!!! pHead->pNext = pLeftpHead->pNext; delete pLeftpHead; delete pRightHead;}int main(){ SNode* pHead = new SNode(0); pHead->pNext = NULL; for (int i = 0; i < 10; i++) { SNode* p = new SNode(rand() % 10); p->pNext = pHead->pNext; pHead->pNext = p; } Print(pHead); Partition(pHead, 3); Print(pHead); Destroy(pHead); return 0;}
阅读全文
1 0
- Partition List 链表划分区间@LeetCode
- LeetCode(Partition List) 划分链表
- Leetcode 86. Partition List(链表划分)
- 【LeetCode】Partition List 链表划分
- LeetCode OJ 之 Partition List (划分链表)
- leetcode 86. Partition List-链表划分|双指针
- Leetcode 86. Partition List 链表划分 解题报告
- LeetCode 86. Partition List 链表的划分 Python Solution
- LintCode Partition List 链表划分
- LeetCode2.2.3 @ Partition List 链表划分 D3F3
- 96.Partition List-链表划分(容易题)
- LeetCode | Partition List(partition方法处理链表)
- Leetcode Partition List 分割链表
- leetcode Partition List二分链表问题
- LeetCode Partition List(链表分段)
- leetcode:Partition List (链表处理)【面试算法题】
- LeetCode Partition List 按值分段链表 系统分析
- 闲话链表(三) leetcode之Partition List
- C#--继承的关键字
- Python之 __slots__ 的用法
- Android底部菜单-FragmentTabHost实现
- nohup介绍
- ZeroBrane Studio 设置
- 【LeetCode】Partition List 链表划分
- Python之 @property 用法
- 关于jquery里面prop和attr的带示例详细对比,深度理解
- 文件系统学习1 四巨头
- 【LeetCode】Reverse Linked List
- phpstorm命令行运行console
- 28-SpringBoot——核心-数据缓存Cache
- GitChat · 安全 | 如何利用搜索引擎做一些「不可描述」的事情?
- BZOJ2442 修剪草坪