From 9d48751bbdad4ccc3aa6d4bdd2d28b1725f37f7e Mon Sep 17 00:00:00 2001 From: Arya Pandey <72682508+arya312@users.noreply.github.com> Date: Thu, 20 Oct 2022 16:49:23 +0530 Subject: [PATCH 1/3] Create merge-two-sorted-linked-lists.cpp --- .../Cpp/merge-two-sorted-linked-lists.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 LinkedList/Cpp/merge-two-sorted-linked-lists.cpp diff --git a/LinkedList/Cpp/merge-two-sorted-linked-lists.cpp b/LinkedList/Cpp/merge-two-sorted-linked-lists.cpp new file mode 100644 index 0000000..bf101b5 --- /dev/null +++ b/LinkedList/Cpp/merge-two-sorted-linked-lists.cpp @@ -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; + } +}; From 4b50f5c3ae7c95e08f3cf30043b134ab74e0e486 Mon Sep 17 00:00:00 2001 From: Arya Pandey <72682508+arya312@users.noreply.github.com> Date: Thu, 20 Oct 2022 16:55:03 +0530 Subject: [PATCH 2/3] flatten binary tree to linked list --- .../flatten-binary-tree-to-linked-list.cpp | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 LinkedList/Cpp/flatten-binary-tree-to-linked-list.cpp diff --git a/LinkedList/Cpp/flatten-binary-tree-to-linked-list.cpp b/LinkedList/Cpp/flatten-binary-tree-to-linked-list.cpp new file mode 100644 index 0000000..91a646d --- /dev/null +++ b/LinkedList/Cpp/flatten-binary-tree-to-linked-list.cpp @@ -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; + + } +}; From 79f723e434c6b92681c4f9f4b5c388085a2b7e83 Mon Sep 17 00:00:00 2001 From: Arya Pandey <72682508+arya312@users.noreply.github.com> Date: Thu, 20 Oct 2022 17:06:30 +0530 Subject: [PATCH 3/3] insertion sort in a linked list --- LinkedList/Cpp/insertion-sort-list.cpp | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 LinkedList/Cpp/insertion-sort-list.cpp diff --git a/LinkedList/Cpp/insertion-sort-list.cpp b/LinkedList/Cpp/insertion-sort-list.cpp new file mode 100644 index 0000000..af1ea25 --- /dev/null +++ b/LinkedList/Cpp/insertion-sort-list.cpp @@ -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; + } +};