From 583f922ece0b27abfc654d5a24b98115302f7e5a Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Mon, 22 Jan 2024 02:03:50 +0530 Subject: [PATCH 1/3] day 22-q3 (py3, cpp) solution added #440 --- Day 22/q3/Tech-neophyte--cp.md | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Day 22/q3/Tech-neophyte--cp.md diff --git a/Day 22/q3/Tech-neophyte--cp.md b/Day 22/q3/Tech-neophyte--cp.md new file mode 100644 index 00000000..4130076d --- /dev/null +++ b/Day 22/q3/Tech-neophyte--cp.md @@ -0,0 +1,72 @@ +## Cpp code: merge sort +``` +/** + * 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: + /// Merge Function + + ListNode *mergeList(ListNode *l1, ListNode *l2){ + ListNode *prev=new ListNode(-1), *curr=prev; + while(l1 and l2){ + if(l1->val<=l2->val){ + curr->next=l1; + l1=l1->next; + }else{ + curr->next=l2; + l2=l2->next; + } + curr=curr->next; + } + if(l1){ + curr->next=l1; + l1=l1->next; + } + if(l2){ + curr->next=l2; + l2=l2->next; + } + return prev->next; + } + + /// Divide and Conquer Approach (Merge Sort) + + ListNode* sortList(ListNode* head){ + if(!head or !head->next) return head; + ListNode *slow=head, *fast=head, *temp=NULL; + while(fast and fast->next){ + temp=slow; + slow=slow->next; + fast=fast->next->next; + } + temp->next=NULL; + + ListNode *point1=sortList(head), *point2=sortList(slow); + return mergeList(point1,point2); + } +}; +``` +## Python code: (Using lists) +``` +class Solution: + def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: + curr = head + l =[] + while curr: + l.append(curr.val) + curr = curr.next + l.sort() + curr = head + for i in l: + curr.val = i + curr = curr.next + return head +``` From 6930d835af81d0f86ee07194816654efc67f3555 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:36:14 +0530 Subject: [PATCH 2/3] moved Tech-neophyte--cp.md to day 22-q3 --- Day-22/q3: Sort List/Tech-neophyte--cp.md | 72 +++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Day-22/q3: Sort List/Tech-neophyte--cp.md diff --git a/Day-22/q3: Sort List/Tech-neophyte--cp.md b/Day-22/q3: Sort List/Tech-neophyte--cp.md new file mode 100644 index 00000000..4130076d --- /dev/null +++ b/Day-22/q3: Sort List/Tech-neophyte--cp.md @@ -0,0 +1,72 @@ +## Cpp code: merge sort +``` +/** + * 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: + /// Merge Function + + ListNode *mergeList(ListNode *l1, ListNode *l2){ + ListNode *prev=new ListNode(-1), *curr=prev; + while(l1 and l2){ + if(l1->val<=l2->val){ + curr->next=l1; + l1=l1->next; + }else{ + curr->next=l2; + l2=l2->next; + } + curr=curr->next; + } + if(l1){ + curr->next=l1; + l1=l1->next; + } + if(l2){ + curr->next=l2; + l2=l2->next; + } + return prev->next; + } + + /// Divide and Conquer Approach (Merge Sort) + + ListNode* sortList(ListNode* head){ + if(!head or !head->next) return head; + ListNode *slow=head, *fast=head, *temp=NULL; + while(fast and fast->next){ + temp=slow; + slow=slow->next; + fast=fast->next->next; + } + temp->next=NULL; + + ListNode *point1=sortList(head), *point2=sortList(slow); + return mergeList(point1,point2); + } +}; +``` +## Python code: (Using lists) +``` +class Solution: + def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: + curr = head + l =[] + while curr: + l.append(curr.val) + curr = curr.next + l.sort() + curr = head + for i in l: + curr.val = i + curr = curr.next + return head +``` From 37cbde43dbc62ab2a21ab4c4ae91b5427edb64c7 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Wed, 24 Jan 2024 11:36:48 +0530 Subject: [PATCH 3/3] Delete Day 22/q3 directory --- Day 22/q3/Tech-neophyte--cp.md | 72 ---------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 Day 22/q3/Tech-neophyte--cp.md diff --git a/Day 22/q3/Tech-neophyte--cp.md b/Day 22/q3/Tech-neophyte--cp.md deleted file mode 100644 index 4130076d..00000000 --- a/Day 22/q3/Tech-neophyte--cp.md +++ /dev/null @@ -1,72 +0,0 @@ -## Cpp code: merge sort -``` -/** - * 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: - /// Merge Function - - ListNode *mergeList(ListNode *l1, ListNode *l2){ - ListNode *prev=new ListNode(-1), *curr=prev; - while(l1 and l2){ - if(l1->val<=l2->val){ - curr->next=l1; - l1=l1->next; - }else{ - curr->next=l2; - l2=l2->next; - } - curr=curr->next; - } - if(l1){ - curr->next=l1; - l1=l1->next; - } - if(l2){ - curr->next=l2; - l2=l2->next; - } - return prev->next; - } - - /// Divide and Conquer Approach (Merge Sort) - - ListNode* sortList(ListNode* head){ - if(!head or !head->next) return head; - ListNode *slow=head, *fast=head, *temp=NULL; - while(fast and fast->next){ - temp=slow; - slow=slow->next; - fast=fast->next->next; - } - temp->next=NULL; - - ListNode *point1=sortList(head), *point2=sortList(slow); - return mergeList(point1,point2); - } -}; -``` -## Python code: (Using lists) -``` -class Solution: - def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: - curr = head - l =[] - while curr: - l.append(curr.val) - curr = curr.next - l.sort() - curr = head - for i in l: - curr.val = i - curr = curr.next - return head -```