Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten binary tree to linked list #121

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions LinkedList/Cpp/flatten-binary-tree-to-linked-list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
TreeNode* prev = NULL;
public:
void flatten(TreeNode* root) {
if(!root) return;

flatten(root->right);
flatten(root->left);

root->right = prev;
root->left = NULL;
prev = root;

}
};
32 changes: 32 additions & 0 deletions LinkedList/Cpp/insertion-sort-list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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* insertionSortList(ListNode* head) {
ListNode* start = new ListNode(0);
start->next = head;
ListNode* cur = head, * prev = start;
while(cur){
if(cur->next and cur->next->val < cur->val){
while(prev->next and prev->next->val < cur->next->val)
prev = prev->next;
ListNode* temp = prev->next;
prev->next = cur->next;
cur->next = cur->next->next;
prev->next->next = temp;
prev = start;
}
else
cur = cur->next;
}
return start->next;
}
};
34 changes: 34 additions & 0 deletions LinkedList/Cpp/merge-two-sorted-linked-lists.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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* removeNthFromEnd(ListNode* head, int n) {
if(head == NULL) return NULL;
if(head -> next == NULL and n == 1) return NULL;
ListNode * temp = head;
int count = 1;
while(temp -> next != NULL){
temp = temp -> next;
count++;
}
if(count == n) return head -> next;
ListNode * ans = head;
int count2 = count;
while(ans -> next != NULL){
if(count2 == n+1){
ans -> next = ans -> next -> next;
break;
}
else {ans = ans -> next; count2--;}
}
return head;
}
};