From e39bcd2c381c86d43d280048814e82115542136a Mon Sep 17 00:00:00 2001 From: Mudit Mahajan Date: Tue, 3 May 2022 13:31:56 +0000 Subject: [PATCH] Complete Day5 --- Day05/Add Two Numbers (Linked List).cpp | 24 ++++++ Day05/Delete Node in a Linked List.cpp | 4 + Day05/Merge Two Sorted Lists.cpp | 27 +++++++ Day05/README.md | 91 ++++++++++++++++++++++ Day05/Remove Nth Node From End of List.cpp | 19 +++++ 5 files changed, 165 insertions(+) create mode 100644 Day05/Add Two Numbers (Linked List).cpp create mode 100644 Day05/Delete Node in a Linked List.cpp create mode 100644 Day05/Merge Two Sorted Lists.cpp create mode 100644 Day05/README.md create mode 100644 Day05/Remove Nth Node From End of List.cpp diff --git a/Day05/Add Two Numbers (Linked List).cpp b/Day05/Add Two Numbers (Linked List).cpp new file mode 100644 index 0000000..2a11a18 --- /dev/null +++ b/Day05/Add Two Numbers (Linked List).cpp @@ -0,0 +1,24 @@ +ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + ListNode* start = new ListNode(); + ListNode* temp = start; + int carry = 0; + + while(l1 or l2 or carry) { + int sum = 0; + if(l1) { + sum += l1 -> val; + l1 = l1 -> next; + } + if(l2) { + sum += l2 -> val; + l2 = l2 -> next; + } + sum += carry; + carry = sum / 10; + ListNode* node = new ListNode(sum % 10); + temp -> next = node; + temp = temp -> next; + } + + return start -> next; + } \ No newline at end of file diff --git a/Day05/Delete Node in a Linked List.cpp b/Day05/Delete Node in a Linked List.cpp new file mode 100644 index 0000000..4c3bbb0 --- /dev/null +++ b/Day05/Delete Node in a Linked List.cpp @@ -0,0 +1,4 @@ +void deleteNode(ListNode* node) { + node -> val = node -> next -> val; + node -> next = node -> next -> next; + } diff --git a/Day05/Merge Two Sorted Lists.cpp b/Day05/Merge Two Sorted Lists.cpp new file mode 100644 index 0000000..3d64d92 --- /dev/null +++ b/Day05/Merge Two Sorted Lists.cpp @@ -0,0 +1,27 @@ +ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { + ListNode* curr1 = list1; + ListNode* curr2 = list2; + + ListNode* ans = new ListNode(); + ListNode* curr = ans; + + while(curr1 and curr2) { + if(curr1 -> val < curr2 -> val) { + curr -> next = curr1; + curr1 = curr1 -> next; + } else { + curr -> next = curr2; + curr2 = curr2 -> next; + } + curr = curr -> next; + } + + if(curr1) { + curr -> next = curr1; + } + if(curr2) { + curr -> next = curr2; + } + + return ans -> next; + } diff --git a/Day05/README.md b/Day05/README.md new file mode 100644 index 0000000..fd55fb2 --- /dev/null +++ b/Day05/README.md @@ -0,0 +1,91 @@ +# Day5 + +## Reverse Linked List + +Link: [https://leetcode.com/problems/reverse-linked-list/] + +- Initialize three pointers, prev and temp as null and curr as head. +- While the curr is not null, do: + - Assign next of curr to temp. + - Assign prev to next of curr. + - Assign curr to prev + - Assign temp to curr +- Return prev + +Time Complexity: O(n) + +## Middle of the Linked List + +Link: [https://leetcode.com/problems/middle-of-the-linked-list/] + +- Use two pointers slow and fast. +``` +slow = slow -> next +fast = fast -> next -> next +``` +- Run the while while both fast and fast -> next are valid. +- Return the slow pointer. + +Time Complexity: O(n) + +## Delete Node in a Linked List + +Link: [https://leetcode.com/problems/delete-node-in-a-linked-list/] + +- Change the given node's value to the value of the next node. +- Change the next address of node to next -> next. +``` +node -> next = node -> next -> next +``` + +Time Complexity: O(1) + +## Remove Nth Node From End of List + +Link: [https://leetcode.com/problems/remove-nth-node-from-end-of-list/] + +- Create a new dummy node start. +- Put the head node in start -> next +- Run a fast pointer for n times. +- Run a slow and fast pointers while fast is not null. +- Delete the node by marking +``` +slow -> next = slow -> next -> next +``` +- Return start -> next + +Time Complexity: O(n) + +## Add Two Numbers (Linked List) + +Link: [https://leetcode.com/problems/add-two-numbers/] + +- Create a dummy node and a temp node pointing to dummy node. +- Initialize a carry variable. +- Run a while loop while either: + - l1 is not empty. + - l2 is not empty. + - Carry is not zero. +- If l1 is not null, add it's value to sum. +- If l2 is not null, add it's value to sum. +- If carry is not zero, add it's value to sum. +- Set the carry as sum / 10. +- Create a new node with sum % 10 as the value. +- Append the new node to the next of temp pointer. +- Return start -> next as the result. + +Time Complexity: O(max(n, m)) + +## Merge two sorted Linked Lists + +Link: [https://leetcode.com/problems/merge-two-sorted-lists/] + +## Merge Two Sorted Lists + +Link: [https://leetcode.com/problems/merge-two-sorted-lists/] + +- Create a dummy node ans and a curr pointer to ans. +- Run a while loop while both list1 and list are not null +- Compare the current element of the two lists and append the smaller one to the curr pointer. +- At the end of the loop, if there are elements present in either list, append the whole list to the curr pointer. +- Return ans -> next as the result. diff --git a/Day05/Remove Nth Node From End of List.cpp b/Day05/Remove Nth Node From End of List.cpp new file mode 100644 index 0000000..a28f191 --- /dev/null +++ b/Day05/Remove Nth Node From End of List.cpp @@ -0,0 +1,19 @@ +ListNode* removeNthFromEnd(ListNode* head, int n) { + ListNode* start = new ListNode(); + start -> next = head; + ListNode* slow = start; + ListNode* fast = start; + + for(int i = 0; i < n; i++) { + fast = fast -> next; + } + + while(fast -> next) { + fast = fast -> next; + slow = slow -> next; + } + + slow -> next = slow -> next -> next; + + return start -> next; + }