其他分享
首页 > 其他分享> > 力扣 题目86- 分隔链表

力扣 题目86- 分隔链表

作者:互联网

题目

题解

我们把一个链表 分成小于 x 的节点的链表与大于或等于 x 的节点的链表 最后合并不就可以了

1         //小于 x 的节点的链表
2         ListNode* lesslistfirst = new(ListNode);
3         lesslistfirst->val = -101;
4         ListNode* lesslister = lesslistfirst;
5         //大于或等于 x 的节点的链表
6         ListNode* greaterlistfirst = new(ListNode);
7         greaterlistfirst->val = -101;
8         ListNode* greaterlister = greaterlistfirst;

小于x就添加到lesslistfirst 大于等于x就添加到greaterlistfirst

 1  for (int i = 0; head != NULL; i++) {
 2             if (head->val < x) {
 3                 lesslister->next = head;
 4                 lesslister = lesslister->next;
 5             }
 6             else
 7             {
 8                 greaterlister->next = head;
 9                 greaterlister = greaterlister->next;
10             }
11             head = head->next;
12         }

合并

 1 greaterlister->next = NULL; 2 lesslister->next = greaterlistfirst->next; 

代码

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 struct ListNode {
 6     int val;
 7     ListNode* next;
 8     ListNode() : val(0), next(nullptr) {}
 9     ListNode(int x) : val(x), next(nullptr) {}
10     ListNode(int x, ListNode* next) : val(x), next(next) {}
11 };
12 
13 class Solution {
14 public:
15     ListNode* partition(ListNode* head, int x) {
16         if (head == nullptr) {
17             return head;
18         }
19         //小于 x 的节点的链表
20         ListNode* lesslistfirst = new(ListNode);
21         lesslistfirst->val = -101;
22         ListNode* lesslister = lesslistfirst;
23         //大于或等于 x 的节点的链表
24         ListNode* greaterlistfirst = new(ListNode);
25         greaterlistfirst->val = -101;
26         ListNode* greaterlister = greaterlistfirst;
27 
28         for (int i = 0; head != NULL; i++) {
29             if (head->val < x) {
30                 lesslister->next = head;
31                 lesslister = lesslister->next;
32             }
33             else
34             {
35                 greaterlister->next = head;
36                 greaterlister = greaterlister->next;
37             }
38             head = head->next;
39         }
40         greaterlister->next = NULL;
41         lesslister->next = greaterlistfirst->next;
42         return lesslistfirst->next;
43     }
44 };
45 
46 
47 void listnum(ListNode& list, vector<int>& vectorlist) {
48     ListNode* headlist = &list;
49     for (int i = 0; i < vectorlist.size(); i++) {
50         ListNode* p;
51         p = (struct ListNode*)malloc(sizeof(struct ListNode*));
52         p->val = vectorlist[i];
53         headlist->next = p;
54         headlist = headlist->next;
55     }
56     headlist->next = NULL;
57 }
58 int main() {
59     Solution sol;
60     ListNode list1(1);
61     vector<int> vectorlist1 = { 4,3,2,5,2 };
62     listnum(list1, vectorlist1);
63     ListNode* list = sol.partition(&list1,3);
64     for (int i = 0; list != NULL; i++) {
65         cout << list->val << endl;
66         list = list->next;
67     }
68 }
View Code

 

标签:力扣,head,ListNode,val,greaterlister,next,链表,lesslister,86
来源: https://www.cnblogs.com/zx469321142/p/16461038.html