C++ 版

/*************************************************************************> File Name: 023.将单向链表按照某值划分为区域.cpp> Author: Maureen > Mail: Maureen@qq.com > Created Time: 一  6/13 11:58:01 2022************************************************************************/#include <iostream>
#include <vector>
using namespace std;class Node {public:int value;Node *next;Node(int v) : value(v) {  }
};//方法一:将链表放入数组,在数组上做partition
void swap(vector<Node *> &arr, int i, int j) {Node *tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;
}void arrPartition(vector<Node*> &arr, int pivot) {if (arr.size() <= 1) return ;int small = -1;int big = arr.size();int index = 0;while (index < big) {if (arr[index]->value < pivot) {swap(arr, ++small, index++);} else if (arr[index]->value > pivot) {swap(arr, --big, index);} else {index++;}}
}Node *listPartition1(Node *head, int pivot) {if (head == nullptr) return nullptr;Node *cur = head;int i = 0;//统计节点个数while (cur != nullptr) {i++;cur = cur->next;}//将链表节点放入数组中vector<Node *> nodeArr(i);i = 0;cur = head;for ( ; i != nodeArr.size(); i++) {nodeArr[i] = cur;cur = cur->next;}//数组完成分区arrPartition(nodeArr, pivot);//数组中的节点串成链表for (i = 1; i != nodeArr.size(); i++) {nodeArr[i - 1]->next = nodeArr[i];}nodeArr[i - 1]->next = nullptr;return nodeArr[0];
}//方法二:分成小、中、大三部分,再把各个部分之间串起来
Node *listPartition2(Node *head, int pivot) {Node *sH = nullptr; //小于区域头结点Node *sT = nullptr; //小于区域尾结点Node *eH = nullptr; //等于区域头结点Node *eT = nullptr; //等于区域尾结点Node *bH = nullptr; //大于区域头结点Node *bT = nullptr; //大于区域尾结点Node *next = nullptr;//将节点分发到三个链表中while (head != nullptr) {next = head->next;head->next = nullptr;if (head->value < pivot) {if (sH == nullptr) {sH = head;sT = head;} else {sT->next = head;sT = head;}} else if (head->value > pivot) {if (bH == nullptr) {bH = head;bT = head;} else {bT->next = head;bT = head;}} else {if (eH == nullptr) {eH = head;eT = head;} else {eT->next = head;eT = head;}}head = next;}//小于区域的尾巴 连  等于区域的头,等于区域的尾巴 连 大于区域的头if (sT != nullptr) { //如果有小于区域sT->next = eH;eT = eT == nullptr ? sT : eT; //下一步,谁去连大于区域的头,谁就变成eT}//下一步,一定是用eT 去连 大于区域的头//有等于区域,eT 就是 等于区域的尾结点//无等于区域,eT 就是 小于区域的尾结点if (eT != nullptr) { //eT 尽量不为空的尾结点eT->next = bH;}return sH != nullptr ? sH : (eH != nullptr ? eH : bH);
}//打印链表
void printLinkedList(Node *node) {cout << "Linked List: ";while (node != nullptr) {cout << node->value << " ";node = node->next;}cout << endl;
}int main() {Node *head = new Node(7);head->next = new Node(9);head->next->next = new Node(1);head->next->next->next = new Node(8);head->next->next->next->next = new Node(5);head->next->next->next->next->next = new Node(2);head->next->next->next->next->next->next = new Node(5);printLinkedList(head);//head = listPartition1(head, 4);head = listPartition2(head, 5);printLinkedList(head);return 0;
}

Java 版

public class Code03_SmallerEqualBigger {public static class Node {public int value;public Node next;public Node(int data) {this.value = data;}}//链表放到数组中,在数组中做partitionpublic static Node listPartition1(Node head, int pivot) {if (head == null) {return head;}Node cur = head;int i = 0;while (cur != null) {i++;cur = cur.next;}Node[] nodeArr = new Node[i];i = 0;cur = head;for (i = 0; i != nodeArr.length; i++) {nodeArr[i] = cur;cur = cur.next;}arrPartition(nodeArr, pivot);for (i = 1; i != nodeArr.length; i++) {nodeArr[i - 1].next = nodeArr[i];}nodeArr[i - 1].next = null;return nodeArr[0];}public static void arrPartition(Node[] nodeArr, int pivot) {int small = -1;int big = nodeArr.length;int index = 0;while (index != big) {if (nodeArr[index].value < pivot) {swap(nodeArr, ++small, index++);} else if (nodeArr[index].value == pivot) {index++;} else {swap(nodeArr, --big, index);}}}public static void swap(Node[] nodeArr, int a, int b) {Node tmp = nodeArr[a];nodeArr[a] = nodeArr[b];nodeArr[b] = tmp;}//不使用容器,申请6个变量public static Node listPartition2(Node head, int pivot) {Node sH = null; // small headNode sT = null; // small tailNode eH = null; // equal headNode eT = null; // equal tailNode mH = null; // big headNode mT = null; // big tailNode next = null; // save next node// every node distributed to three listswhile (head != null) {next = head.next;head.next = null;if (head.value < pivot) {if (sH == null) {sH = head;sT = head;} else {sT.next = head;sT = head;}} else if (head.value == pivot) {if (eH == null) {eH = head;eT = head;} else {eT.next = head;eT = head;}} else {if (mH == null) {mH = head;mT = head;} else {mT.next = head;mT = head;}}head = next;}// 小于区域的尾巴,连等于区域的头,等于区域的尾巴连大于区域的头if (sT != null) { // 如果有小于区域sT.next = eH;eT = eT == null ? sT : eT; // 下一步,谁去连大于区域的头,谁就变成eT}// 下一步,一定是需要用eT 去接 大于区域的头// 有等于区域,eT -> 等于区域的尾结点// 无等于区域,eT -> 小于区域的尾结点// eT 尽量不为空的尾巴节点if (eT != null) { // 如果小于区域和等于区域,不是都没有eT.next = mH;}return sH != null ? sH : (eH != null ? eH : mH);}public static void printLinkedList(Node node) {System.out.print("Linked List: ");while (node != null) {System.out.print(node.value + " ");node = node.next;}System.out.println();}public static void main(String[] args) {Node head1 = new Node(7);head1.next = new Node(9);head1.next.next = new Node(1);head1.next.next.next = new Node(8);head1.next.next.next.next = new Node(5);head1.next.next.next.next.next = new Node(2);head1.next.next.next.next.next.next = new Node(5);printLinkedList(head1);// head1 = listPartition1(head1, 4);head1 = listPartition2(head1, 5);printLinkedList(head1);}
}

将单向链表按某值划分成左边小、中间相等、右边大的形式相关推荐

  1. [******] 链表问题:将单向链表按某值划分成左边小、中间相等、右边大的形式...

    问题描述 普通问题:给定一个单向链表的头节点head,节点的值类型是整数,再给定一个整数 pivot,实现一个调整链表的函数: 使得左半部分的值都是小于pivot的节点,中间部分都是等于pivot的节 ...

  2. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(进阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.调整后的节点顺序要保持与原链表中节点的先后次序一 ...

  3. 链表问题8——将单向链表按某值划分成左边小、中间相等、右边大的形式(初阶)

    题目 给定一个单向链表头节点head,和一个整数pivot. 实现一个调整链表的函数,将链表调整为左部分小于pivot,中间等于,右边大于pivot的.对调整后的节点顺序没有更多的要求 链表9-> ...

  4. 将单向链表按某值划分成左边小、 中间相等、 右边大的形式~迎娶是挺

    这道题一开始想到的方法可能就是patition方法了,大概思路我说一下,先把这个链表存为数组(说明其空间复杂度为0(1)),然后在对这个数组进行patition,即定义两个指针,一个指向数组的-1位置 ...

  5. 将数组排列成左边小,中间相等,右边大的形式 给定链表节点数组和某个值

    题目出自左程云书 原题是将给定一个链表和一个数,将其排列成左边小, 中间等于,右边大于某个数的形式 解:将链表的节点先存入一个Node数组,将数组排序,再将数组中的链表节点连接起来 以下是节点排序函数 ...

  6. 将单向链表按某值分成左边小、中间相等、右边大的形式

    [题目] 给定一个单向链表的头节点head,节点的值类型是整型,再给定一个 整 数pivot 实现一个调整链表的函数,将链表调整为左部分都是值小于 pivot 的节点,中间部分都是值等于pivot的节 ...

  7. java数组转换成单链表_【转】单向链表(单链表)的Java实现

    最近被问到链表,是一个朋友和我讨论Java的时候说的.说实话,我学习编程的近一年时间里,学到的东西还是挺少的.语言是学了Java和C#,关 于Web的学了一点Html+css+javascript.因 ...

  8. 6-4 学生成绩链表处理 (20分) 本题要求实现两个函数,一个将输入的学生成绩组织成单向链表;另一个将成绩低于某分数线的学生结点从链表中删除。 函数接口定义: ```cpp struct stu

    6-4 学生成绩链表处理 (20分) 本题要求实现两个函数,一个将输入的学生成绩组织成单向链表:另一个将成绩低于某分数线的学生结点从链表中删除. 函数接口定义: struct stud_node *c ...

  9. 2. 输入若干个学生信息(包括学号、姓名和某科成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。 输入输出示例: 1 Zhang 78 2 Wang

    输入若干个学生信息(包括学号.姓名和某科成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出. 输入输出示例: 1 Zhang 78 2 Wang 80 ...

最新文章

  1. 高并发服务优化篇:从RPC预热转发看服务端性能调优
  2. redis主从复制搭建
  3. mysql %type_mysql 存储过程 %type
  4. Python字符串常用方法(二)
  5. CTFshow 命令执行 web40
  6. python扩展库简介_python非官方扩展库
  7. nginx 代理多个服务器——多个server方式
  8. Oracle性能调优
  9. L1-051 打折-PAT团体程序设计天梯赛GPLT
  10. CentOS7安装Portainer实现docker可视化操作
  11. CVPR 2021 | 谷歌发布视频全景分割数据集!顺带开发个模型屠榜
  12. JAVA 实现《俄罗斯方块》游戏|CSDN创作打卡
  13. QT实现年会抽奖小软件的实现
  14. 游戏专用Win10 64位 竞技超棒专业版
  15. 列主元Guass消去法
  16. 用计算机绘制滴定曲线,利用Excel系统绘制酸碱滴定曲线
  17. 明源云与华为联合发起828 B2B企业节,共同成就好生意!
  18. 手机连不上wifi,一直显示正在获取ip地址
  19. 使用Huginn批量订阅微信公众号
  20. 测试点 - 发朋友圈

热门文章

  1. 港科夜闻|香港科大张明杰教授课题组报道相分离介导突触前膜活性区的组织方式...
  2. Python笔记:纯python操作矩阵:进行矩阵的相乘运算
  3. 2020东京奥运会金牌榜爬虫
  4. vscode查看外部依赖包的源码
  5. 等了15年,这本豆瓣评分高达9.3的编程巨著终于出版了!
  6. 输出一个区间内的质数(素数)
  7. JavaScript(作用域链)
  8. JD狗东邀新活动余额兑换软件及使用教程
  9. C++单例模式与线程安全
  10. maven导入orcal坐标 报错 :Cannot resolve com.oracle:ojdbc14:11.2.0.1.0