leetcode链表--11、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,Given1->4->3->2->5->2and x = 3,

return1->2->2->4->3->5

解题思路:.本题目是将小于x的放在左侧,大于等于x的放在后面,且相对顺序不变

1)定义两个链表   p存小于x的  q存大于等于x的

2)存完后q->next =  NULL

3)p1->next = qhead->next;

 1 #include <iostream>
 2 #include <malloc.h>
 3 using namespace std;
 4 struct ListNode {
 5     int val;
 6     ListNode *next;
 7     ListNode(int x) : val(x), next(NULL) {}
 8 };
 9 class Solution {
10 public:
11     //定义两个链表  1个存小于x的  一个存大于等于x的
12     //连接两个链表
13     ListNode *partition(ListNode *head, int x) {
14         ListNode *phead = new ListNode(0);
15         ListNode *qhead = new ListNode(0);
16         ListNode *p = phead;
17         ListNode *q = qhead;
18         while(head != NULL)
19         {
20             if(head->val < x)
21             {
22                 p->next = head;
23                 p = p->next;
24             }
25             else
26             {
27                 q->next = head;
28                 q = q->next;
29             }
30             head = head->next;
31         }
32         q->next = NULL;//别忘记给q之后的链表断开,否则无限循环
33         p->next = qhead->next;
34         return phead->next;
35     }
36 };
37 ListNode *CreateList(int n)
38 {
39     ListNode *head;
40     ListNode *p,*pre;
41     int i;
42     head=(ListNode *)malloc(sizeof(ListNode));
43     head->next=NULL;
44     pre=head;
45     for(i=1;i<=n;i++)
46     {
47         p=(ListNode *)malloc(sizeof(ListNode));
48         cin>>p->val;
49         pre->next=p;
50         pre=p;
51     }
52     p->next=NULL;
53
54     return head->next;
55 }
56 /*-------------------------输出链表-----------------------------------*/
57 void PrintList(ListNode *h)
58 {
59     ListNode *p;
60
61     p=h;//不带空的头结点
62     while(p)
63     {
64         cout<<p->val<<" ";
65         p=p->next;
66         cout<<endl;
67     }
68 }
69 int main()
70 {
71     int n1;
72     int x;
73     ListNode *h1;
74     cout<<"输入链表1的结点数目"<<endl;
75     cin>>n1;
76     h1 = CreateList(n1);
77     cout<<"链表1为:"<<endl;
78     PrintList(h1);
79     cout<<"输入x"<<endl;
80     cin>>x;
81     Solution s;
82     h1 = s.partition(h1,x);
83     cout<<"分区后链表1为:"<<endl;
84     PrintList(h1);
85     return 0;
86 }

时间: 2024-08-10 01:50:01

leetcode链表--11、partition-list(链表分区)的相关文章

Leetcode: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,Given

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the va

Leetcode:Sort List 对单链表归并排序

Sort a linked list in O(n log n) time using constant space complexity. 看到O(n log n)的排序算法,适合单链表的首先想到的就是归并排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cla

Leetcode 206 反转一个单链表

Leetcode 206 反转一个单链表 分析 L->M->R->- 操作: L->NIL M->L ==>> M->R->R2->- 判断是否需要继续在R2节点的基础上向右移动: R2.next==NIL 例题 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 分析 L M R NIL NIL head// hea

剑指offer和leetcode都有的_反转链表

输入一个链表,反转链表后,输出链表的所有元素. 一开始我的做法非常复杂,因为我一次变了两个指针,导致要分别判断单数个结点和偶数个结点的情况. 反转链表要解决的两个问题就是 1.结点指向它的前驱 2.头结点变为其尾结点 当反转一个结点时,假设反转i,首先需要记录它的前驱,让它指向它的前驱,还需要记录它的后继结点,否则就会造成链表的断裂,所以至少需要三个结点. 后来我整理了一下思路,发现有两种方法,一种用的是循环,一种用的是递归. 1.循环做法 /**循环做法来反转链表 * 思路是用三个指针来记录,

【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

题目说明 https://leetcode-cn.com/problems/partition-list/description/ 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 解法1 遍历链表,记录最后一个小于x的结点less,当遍历到的结点小于x时,将该点插入到less之后,直到遍历结束. /* * 时间复杂度:O(n) * 记录最后一低点小于x的点less * 遍历链表,当遍历到的点(p

线性时间将两个有序链表合成一个有序链表(constant additional space)

description: given two sorted singly list, merge them into one using constant additional space algorithm: we will reference the two linked list as list1 and list2 for convenience, since list1 is sorted,just find the right position for each element in