Leetcode-partition list

Source: Internet
Author: User

Question: partition list

Given a linked list and a valueX, Partition it such that all nodes lessXCome before nodes greater than or equalX.

You shoshould preserve the original relative order of the nodes in each of the two partitions.

For example,
Given1->4->3->2->5->2AndX= 3,
Return1->2->2->4->3->5.

The question means that, without changing the relative position of the original sequence, put the smaller part than X in the front, and the rest in the back

 

Personal thoughts:

1. Based on whether the first node of the sequence is less than X, it is divided into two cases. If the node is less than X, the whole sequence is regarded as two parts. The first part is a sequence smaller than X, the second part has not been traversed. When we traverse the second part, place the nodes smaller than X in the second part at the end of the first part until the second part is traversed, in this case, the second part is the sequence with the size equal to X. If the size equal to X, the first part is the sequence with the size equal to X. When traversing the second part, place the node with the size equal to X at the end of the first part, at the end of the traversal, you can reverse the two parts.

2. Pay attention to the boundary.

Code:

1/* 2 # include <stddef. h> 3 4 struct listnode 5 {6 int val; 7 listnode * Next; 8 listnode (int x): Val (x), next (null) {}; 9 }; 10 */11 class solution 12 {13 public: 14 listnode * partition (listnode * head, int X) 15 {16 if (! Head) 17 {18 return NULL; 19} 20 21 bool flag; // whether the value of the first node is less than x 22 23 if (Head-> Val <X) 24 {25 flag = true; 26} 27 else 28 {29 flag = false; 30} 31 32 If (FLAG) 33 {34 listnode * Less = head; 35 listnode * Before = head; 36 listnode * Current = head-> next; 37 38 While (current) 39 {40 if (current-> Val <X) 41 {42 if (before! = Less) 43 {44 before-> next = Current-> next; 45 current-> next = less-> next; 46 less-> next = current; 47 less = less-> next; 48 current = before-> next; 49} 50 else 51 {52 less = less-> next; 53 before = before-> next; 54 current = Current-> next; 55} 56} 57 else 58 {59 Before = before-> next; 60 current = Current-> next; 61} 62} 63} 64 else 65 {66 listnode * Greater = head; 67 listnode * Before = Head; 68 listnode * Current = head-> next; 69 70 while (current) 71 {72 If (current-> Val> = x) 73 {74 if (before! = Greater) 75 {76 before-> next = Current-> next; 77 current-> next = greater-> next; 78 greater-> next = current; 79 greater = greater-> next; 80 current = before-> next; 81} 82 else 83 {84 greater = greater-> next; 85 before = before-> next; 86 current = Current-> next; 87} 88} 89 else 90 {91 before = before-> next; 92 current = Current-> next; 93} 94} 95 96 If (before! = Greater) // indicates that there are nodes smaller than X in the linked list and place them in front of the node larger than or equal to X 97 {98 before-> next = head; 99 head = greater-> next; 100 greater-> next = NULL; 101} 102} 103 104 return head; 105} 106 };
View code

 

I also saw another idea on the Internet. I needed to consume O (n) memory and create two linked lists to store nodes smaller than X and greater than x respectively. After traversing the original sequence, merge the two linked lists.

Leetcode-partition list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.