diff --git a/cpp/23-Merge-K-Sorted-Lists.cpp b/cpp/23-Merge-K-Sorted-Lists.cpp new file mode 100644 index 000000000..7f61d8afa --- /dev/null +++ b/cpp/23-Merge-K-Sorted-Lists.cpp @@ -0,0 +1,51 @@ +/** + * Definition for singly-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 compare{ + public: + bool operator()(ListNode* a,ListNode* b){ + return a->val>b->val; + } +}; + +class Solution { +public: + ListNode* mergeKLists(vector& lists) { + + priority_queue,compare> pq; + + for(int i=0;inext=curr; + last=last->next; + + if(curr->next!=NULL){ + pq.push(curr->next); + } + } + return dummy->next; + } +}; \ No newline at end of file diff --git a/cpp/25-Reverse-Nodes-in-k-Group.cpp b/cpp/25-Reverse-Nodes-in-k-Group.cpp new file mode 100644 index 000000000..d7694611d --- /dev/null +++ b/cpp/25-Reverse-Nodes-in-k-Group.cpp @@ -0,0 +1,47 @@ +/** + * Definition for singly-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* reverseKGroup(ListNode* head, int k) { + + if(head==NULL || head->next==NULL){ + return head; + } + + ListNode* res=new ListNode(0); + res->next=head; + ListNode* start=res; + stack st; + ListNode* prev=NULL; + while(head!=NULL){ + ListNode* temp=head; + while(st.size()next; + } + if(st.size()==k){ + while(!st.empty()){ + prev=st.top(); + prev->next=NULL; + st.pop(); + start->next=prev; + start=start->next; + } + } + else{ + start->next=temp; + return res->next; + } + } + prev->next=NULL; + return res->next; + } +}; \ No newline at end of file diff --git a/cpp/30-Substring-with-Concatenation-of-All-Words.cpp b/cpp/30-Substring-with-Concatenation-of-All-Words.cpp new file mode 100644 index 000000000..56940b581 --- /dev/null +++ b/cpp/30-Substring-with-Concatenation-of-All-Words.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector findSubstring(string s, vector& words) { + + unordered_map mp; + for(string word:words){ + mp[word]++; + } + + int slen=s.size(); + int wlen=words.size(); + int len=words[0].size(); + + vector ans; + for(int i=0;i seen; + for(int j=0;j