From 93dad5d33e0bbd1168518775b501a08a1a7731b4 Mon Sep 17 00:00:00 2001 From: Anjali Singh <71429859+AnjaliSingh-17@users.noreply.github.com> Date: Thu, 5 Nov 2020 00:14:08 +0530 Subject: [PATCH 1/4] Create Week 2; Day 1 --- Team5/AnjaliSingh/Week 2; Day 1 | 111 ++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Team5/AnjaliSingh/Week 2; Day 1 diff --git a/Team5/AnjaliSingh/Week 2; Day 1 b/Team5/AnjaliSingh/Week 2; Day 1 new file mode 100644 index 0000000..aa03d89 --- /dev/null +++ b/Team5/AnjaliSingh/Week 2; Day 1 @@ -0,0 +1,111 @@ +//Remove_Nth_Node_From_End_Of_List + + + +//Merge_Two_Sorted_Lists + +/** + * 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* mergeTwoLists(ListNode* l1, ListNode* l2) { + + ListNode* l3; + + if(l1==NULL) + return l2; + else if(l2==NULL) + return l1; + + if(l1->val < l2->val) + { + l3 = l1; + l3->next = mergeTwoLists(l1->next,l2); + } + else + { + l3 = l2; + l3->next = mergeTwoLists(l1,l2->next); + } + return l3; + } +}; + +//Remove_Duplicates_From_Sorted_List + +/** + * 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* deleteDuplicates(ListNode* head) { + ListNode *prev=head,*next; + + if(!head) + return NULL; + else + next = head->next; + + while(next!=NULL) + { + if(prev->val != next->val) + { + prev = next; + next = next->next; + } + else + { + prev->next = next->next; + delete next; + next = prev->next; + } + } + return head; + } +}; + +//Linked_List_Cycle + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) + { + ListNode *fast, *slow; + fast = slow = head; + while (fast != NULL && slow != NULL) + { + if (fast->next != NULL) + fast = fast->next->next; + else + fast = NULL; + slow = slow->next; + if (fast != NULL && fast == slow) + return true; + } + return false; + } +}; + From 31aa876f4382d1415154a12cf213503c2cdbf84d Mon Sep 17 00:00:00 2001 From: Anjali Singh <71429859+AnjaliSingh-17@users.noreply.github.com> Date: Sat, 7 Nov 2020 23:20:25 +0530 Subject: [PATCH 2/4] Update Week 2; Day 1 --- Team5/AnjaliSingh/Week 2; Day 1 | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Team5/AnjaliSingh/Week 2; Day 1 b/Team5/AnjaliSingh/Week 2; Day 1 index aa03d89..3bdbb9d 100644 --- a/Team5/AnjaliSingh/Week 2; Day 1 +++ b/Team5/AnjaliSingh/Week 2; Day 1 @@ -109,3 +109,105 @@ public: } }; +// + + + +// + + + +// + + + +// + + + +// + + + +//Middle_of_Linked_List + +/** + * 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* middleNode(ListNode* head) + { + ListNode* one = head; + ListNode* two = head; + while(two != NULL && two->next != NULL) + { + two = two->next->next; + one = one->next; + } + return one; + } +}; + +//Sort_List + +/** + * 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* sortList(ListNode* head) { + if (head == NULL) return NULL; + vector data; + ListNode *p = head; + while (p) { + data.push_back(p->val); + p = p->next; + } + sort(begin(data), end(data)); + p = head; + for (const auto &n: data) { + p->val = n; + p = p->next; + } + return head; + } +}; + +//Convert_Binary_Number_In_A_Linked_List_To_Integer + +/** + * 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: + int getDecimalValue(ListNode* head) { + long long int value = 0; + while(head!=NULL){ + value = value*2 + head->val; + head = head->next; + } + return value; + } +}; From 5fd0ba3065c5f7acbacd8a6b7041007f356d3354 Mon Sep 17 00:00:00 2001 From: Anjali Singh <71429859+AnjaliSingh-17@users.noreply.github.com> Date: Sat, 7 Nov 2020 23:52:53 +0530 Subject: [PATCH 3/4] Update and rename Week 2; Day 1 to Week 2 --- Team5/AnjaliSingh/Week 2 | 410 ++++++++++++++++++++++++++++++++ Team5/AnjaliSingh/Week 2; Day 1 | 213 ----------------- 2 files changed, 410 insertions(+), 213 deletions(-) create mode 100644 Team5/AnjaliSingh/Week 2 delete mode 100644 Team5/AnjaliSingh/Week 2; Day 1 diff --git a/Team5/AnjaliSingh/Week 2 b/Team5/AnjaliSingh/Week 2 new file mode 100644 index 0000000..9622818 --- /dev/null +++ b/Team5/AnjaliSingh/Week 2 @@ -0,0 +1,410 @@ +//Remove_Nth_Node_From_End_Of_List + +/** + * 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) { + ListNode *temp=head,*slow=head; + if(!head->next) + return NULL; + while(n>0){ + temp=temp->next; + n--; + } + if(temp==NULL) + return head->next; + while(temp->next!=NULL){ + temp=temp->next; + slow=slow->next; + } + slow->next=slow->next->next; + return head; + } +}; + +//Merge_Two_Sorted_Lists + +/** + * 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* mergeTwoLists(ListNode* l1, ListNode* l2) { + + ListNode* l3; + + if(l1==NULL) + return l2; + else if(l2==NULL) + return l1; + + if(l1->val < l2->val) + { + l3 = l1; + l3->next = mergeTwoLists(l1->next,l2); + } + else + { + l3 = l2; + l3->next = mergeTwoLists(l1,l2->next); + } + return l3; + } +}; + +//Remove_Duplicates_From_Sorted_List + +/** + * 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* deleteDuplicates(ListNode* head) { + ListNode *prev=head,*next; + + if(!head) + return NULL; + else + next = head->next; + + while(next!=NULL) + { + if(prev->val != next->val) + { + prev = next; + next = next->next; + } + else + { + prev->next = next->next; + delete next; + next = prev->next; + } + } + return head; + } +}; + +//Linked_List_Cycle + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + bool hasCycle(ListNode *head) + { + ListNode *fast, *slow; + fast = slow = head; + while (fast != NULL && slow != NULL) + { + if (fast->next != NULL) + fast = fast->next->next; + else + fast = NULL; + slow = slow->next; + if (fast != NULL && fast == slow) + return true; + } + return false; + } +}; + +//Intersection_Of_Two_LL + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + + int getLength(ListNode *head){ + + int len = 0; + while(head) + { + head=head->next; + len++; + } + return len; + } + + ListNode* intersectAt(int d, ListNode *headA, ListNode *headB){ + + while(d-->0) + headA = headA->next; + + while(headA && headB) + { + if(headA == headB)return headA; + headA = headA->next; + headB = headB->next; + } + return NULL; + } + + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + + if(headA == NULL || headB == NULL)return NULL; + ListNode *result = NULL; + int l1 = getLength(headA); + int l2 = getLength(headB); + + if(l1>l2) + result = intersectAt(l1-l2, headA, headB); + else + result = intersectAt(l2-l1, headB, headA); + return result; + } +}; + +//Reverse_Linked_List + +/** + * 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* reverseList(ListNode* head) + { + ListNode* curr = head; + ListNode* prev = NULL; + ListNode* next = NULL; + while(curr != NULL) + { + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + return prev; + } +}; + +//Palindrome_LL + +/** + * 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* reverse(ListNode* l) + { + ListNode *curr = l; + ListNode *next = NULL; + ListNode * prev = NULL; + + while(curr){ + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + return prev; + } + + int getLength(ListNode* l){ + ListNode *ptr = l; + int length = 0; + while(ptr){ + length ++; + ptr = ptr->next; + } + return length; + } + + bool isPalindrome(ListNode* head) { + + int length = getLength(head); + if(length < 2) + return true; + int middle = length % 2 ? ceil(length/2) + 1 : length / 2; + + ListNode *middle_ptr = head; + ListNode *head_ptr = head; + while(middle){ + middle --; + middle_ptr = middle_ptr->next; + } + middle_ptr = reverse(middle_ptr); + while(middle_ptr){ + if(middle_ptr->val != head_ptr->val) + return false; + middle_ptr = middle_ptr->next; + head_ptr = head_ptr->next; + } + return true; + } +}; + +//Odd_Even_LL + +/** + * 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* oddEvenList(ListNode* head) { + if(head==NULL || head->next==NULL)return head; + ListNode *p1,*p2,*head1,*head2; + head1=p1=head; + head2=p2=head->next; + int cnt=1; + while(p1 && p2) + { + if(cnt%2!=0) + { + p1->next=p2->next; + p1=p1->next; + } + else + { + p2->next=p1->next; + p2=p2->next; + } + cnt++; + } + p1=head1; + while(p1->next)p1=p1->next; + p1->next=head2; + return head1; + } +}; + +//Middle_of_Linked_List + +/** + * 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* middleNode(ListNode* head) + { + ListNode* one = head; + ListNode* two = head; + while(two != NULL && two->next != NULL) + { + two = two->next->next; + one = one->next; + } + return one; + } +}; + +//Sort_List + +/** + * 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* sortList(ListNode* head) { + if (head == NULL) return NULL; + vector data; + ListNode *p = head; + while (p) { + data.push_back(p->val); + p = p->next; + } + sort(begin(data), end(data)); + p = head; + for (const auto &n: data) { + p->val = n; + p = p->next; + } + return head; + } +}; + +//Convert_Binary_Number_In_A_Linked_List_To_Integer + +/** + * 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: + int getDecimalValue(ListNode* head) { + long long int value = 0; + while(head!=NULL){ + value = value*2 + head->val; + head = head->next; + } + return value; + } +}; diff --git a/Team5/AnjaliSingh/Week 2; Day 1 b/Team5/AnjaliSingh/Week 2; Day 1 deleted file mode 100644 index 3bdbb9d..0000000 --- a/Team5/AnjaliSingh/Week 2; Day 1 +++ /dev/null @@ -1,213 +0,0 @@ -//Remove_Nth_Node_From_End_Of_List - - - -//Merge_Two_Sorted_Lists - -/** - * 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* mergeTwoLists(ListNode* l1, ListNode* l2) { - - ListNode* l3; - - if(l1==NULL) - return l2; - else if(l2==NULL) - return l1; - - if(l1->val < l2->val) - { - l3 = l1; - l3->next = mergeTwoLists(l1->next,l2); - } - else - { - l3 = l2; - l3->next = mergeTwoLists(l1,l2->next); - } - return l3; - } -}; - -//Remove_Duplicates_From_Sorted_List - -/** - * 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* deleteDuplicates(ListNode* head) { - ListNode *prev=head,*next; - - if(!head) - return NULL; - else - next = head->next; - - while(next!=NULL) - { - if(prev->val != next->val) - { - prev = next; - next = next->next; - } - else - { - prev->next = next->next; - delete next; - next = prev->next; - } - } - return head; - } -}; - -//Linked_List_Cycle - -/** - * Definition for singly-linked list. - * struct ListNode { - * int val; - * ListNode *next; - * ListNode(int x) : val(x), next(NULL) {} - * }; - */ -class Solution { -public: - bool hasCycle(ListNode *head) - { - ListNode *fast, *slow; - fast = slow = head; - while (fast != NULL && slow != NULL) - { - if (fast->next != NULL) - fast = fast->next->next; - else - fast = NULL; - slow = slow->next; - if (fast != NULL && fast == slow) - return true; - } - return false; - } -}; - -// - - - -// - - - -// - - - -// - - - -// - - - -//Middle_of_Linked_List - -/** - * 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* middleNode(ListNode* head) - { - ListNode* one = head; - ListNode* two = head; - while(two != NULL && two->next != NULL) - { - two = two->next->next; - one = one->next; - } - return one; - } -}; - -//Sort_List - -/** - * 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* sortList(ListNode* head) { - if (head == NULL) return NULL; - vector data; - ListNode *p = head; - while (p) { - data.push_back(p->val); - p = p->next; - } - sort(begin(data), end(data)); - p = head; - for (const auto &n: data) { - p->val = n; - p = p->next; - } - return head; - } -}; - -//Convert_Binary_Number_In_A_Linked_List_To_Integer - -/** - * 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: - int getDecimalValue(ListNode* head) { - long long int value = 0; - while(head!=NULL){ - value = value*2 + head->val; - head = head->next; - } - return value; - } -}; From d6d543f1ad91d05e3f9ec60067880cd72a20e04c Mon Sep 17 00:00:00 2001 From: Anjali Singh <71429859+AnjaliSingh-17@users.noreply.github.com> Date: Sun, 6 Dec 2020 18:14:55 +0530 Subject: [PATCH 4/4] Create Pascal's Triangle --- Team5/AnjaliSingh/Pascal's Triangle | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Team5/AnjaliSingh/Pascal's Triangle diff --git a/Team5/AnjaliSingh/Pascal's Triangle b/Team5/AnjaliSingh/Pascal's Triangle new file mode 100644 index 0000000..5c5e1be --- /dev/null +++ b/Team5/AnjaliSingh/Pascal's Triangle @@ -0,0 +1,17 @@ +class Solution { +public: + vector > generate(int numRows) + { + vector >ans; + for(int i=0;irow(i+1,1); + for(int j=1;j