Skip to content

Commit

Permalink
Complete Day5
Browse files Browse the repository at this point in the history
  • Loading branch information
muditmahajan21 committed May 3, 2022
1 parent 0bfdbd4 commit e39bcd2
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Day05/Add Two Numbers (Linked List).cpp
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 4 additions & 0 deletions Day05/Delete Node in a Linked List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
void deleteNode(ListNode* node) {
node -> val = node -> next -> val;
node -> next = node -> next -> next;
}
27 changes: 27 additions & 0 deletions Day05/Merge Two Sorted Lists.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
91 changes: 91 additions & 0 deletions Day05/README.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions Day05/Remove Nth Node From End of List.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e39bcd2

Please sign in to comment.