From 4898bc1f606c8d08a009a19d7a35c4e215fdeb3a Mon Sep 17 00:00:00 2001 From: Somasree Majumder <56045049+soma2000-lang@users.noreply.github.com> Date: Wed, 28 Jul 2021 03:51:07 +0530 Subject: [PATCH] Add files via upload Partition List #213.Coded in C++ --- Linked List/C++/10_Partition List.cpp | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Linked List/C++/10_Partition List.cpp diff --git a/Linked List/C++/10_Partition List.cpp b/Linked List/C++/10_Partition List.cpp new file mode 100644 index 0000000..8aba7b1 --- /dev/null +++ b/Linked List/C++/10_Partition List.cpp @@ -0,0 +1,61 @@ + +#include +#include +#include +using namespace std; +//definition of a single linked list + struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} + }; + + + + +class Solution { +public: + ListNode* partition(ListNode* head, int x) { + ListNode *n = new ListNode();//input + ListNode *tail = n;//dummy + ListNode *q = new ListNode(); + ListNode *t = q;//dummy variable + while(head!=NULL){//for iteraing + if(head->val>=x){ + t->next=head; + t=t->next;//the t dummy or storing the nodes which has values greater than or equal to given value + } + else if(head->valnext=head; + tail=tail->next; + } + head=head->next; + } + tail->next=q->next; + t->next=NULL; + return n->next;//printing the final one + } +}; +int main() +{ + + int x,n;//the + cin>>x; + cin>>n; + int head[n]; + for(int i=0;i>head[i]; + } + partition(head, x);//calling the func + return 0; + +} +/*Input: head = [1,4,3,2,5,2], x = 3 +Output: [1,2,2,4,3,5] +Example 2: + +Input: head = [2,1], x = 2 +Output: [1,2] \ No newline at end of file