diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/07-segEvenOddNodes.md b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/07-segEvenOddNodes.md index 11afddf..47c1370 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/07-segEvenOddNodes.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/07-segEvenOddNodes.md @@ -1,6 +1,6 @@ ## Segrregate Odd Even Nodes of a LL. -Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. +Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and **return the reordered list.** **The first node is considered odd, and the second node is even, and so on.** diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/08-sort-0s-1s-2s.md b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/08-sort-0s-1s-2s.md index 407c5f7..bd5fa52 100644 --- a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/08-sort-0s-1s-2s.md +++ b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/08-sort-0s-1s-2s.md @@ -1,6 +1,6 @@ ## Sort the 0s, 1s and 2s in the given LL -Given a LL of N nodes, where each node has an integer value that can be 0, 1, or 2. You need to sort the linked list in non-decreasing order and the return the head of the sorted list. +Given a LL of N nodes, where each node has an integer value that can be 0, 1, or 2. You need to sort the linked list in **non-decreasing order** and the return the head of the sorted list. **Expected TC O(N), SC O(1)** diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/09-delete-nth-node-from-back.md b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/09-delete-nth-node-from-back.md new file mode 100644 index 0000000..b0b1177 --- /dev/null +++ b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/09-delete-nth-node-from-back.md @@ -0,0 +1,77 @@ +## Delete Nth Node from Back in a LL + +Given the head of a linked list, remove the nth node from the end of the list and return its head. + +**Expected TC : O(N), SC : O(1)** + +### Algorithm + +**Brute Force (Two passes)** + +Two reach the node to be deleted from back, we can first traverse the entire LL to know the size (using a counter) and then traverse again till `count - n`th node. + +By this, we can reach the previous node of `n`th node and then we can easily remove the next node (technically `n`th node). + +**TC : O(Length + Length-N) ~ O(2N) _unacceptable_, SC : O(1)** + +```cpp +Node* removeNthFromEnd(Node* head, int n) { + Node* temp = head; + int cnt = 0; + while(temp != nullptr){ + cnt++; + temp = temp->next; + } + temp = head; + int res = cnt - n; + + // Check if the node to be deleted is the head + if (res == 0) { + Node* newHead = head->next; + delete head; + return newHead; + } + + // Move to the node before the one to be deleted + while (temp != nullptr){ + res--; + if (res == 0){ + break; + } + temp = temp->next; + } + + if (temp->next != nullptr) { + Node* deleteNode = temp->next; + temp->next = temp->next->next; + delete deleteNode; + } + return head; +} +``` + +--- + +**Optimal Solution (Two pointer)** + +1. We'll take help of two pointers, `fast` and `slow` both initially pointing to head. +2. The fast pointer will move `n` steps and once it reaches, the slow pointer will start moving simultaneously unless the fast pointer reaches the last node. +3. Once the fast point reaches the last node, the slow node will be at the previous node of `n`th node. +4. Now we can easily remove the next node (technically `n`th node). + +```cpp +Node* removeNthFromEnd(Node* head, int n){ + Node* fast = head; + Node* slow = head; + for (int i = 0; i < n; i++) fast = fast->next; + if (fast == nullptr) return head->next; + while (fast->next != nullptr){ + slow = slow->next; + fast = fast->next; + } + Node* delNode = slow->next; + slow->next = delNode->next; + delete delNode; + return head; +} +``` diff --git a/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/10-reverseLL.md b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/10-reverseLL.md new file mode 100644 index 0000000..3412afb --- /dev/null +++ b/Data-Structures/Linear-Data-Structures/Linked-List/Singly-Linked-List/10-reverseLL.md @@ -0,0 +1,62 @@ +## Reverse the given LL + +Given the head of a singly linked list, reverse the list, and return the reversed list. + +### Algorithm + +**Brute force (Data replacement)** + +1. The most simple way to reverse a LL is to replace the data in it. +2. Create a LIFO data structure (Stack), Traverse the LL, and push all elements in the stack. +3. Traverse the LL again and re-assign the Node data with stack's top elements. +4. The LL would be reversed, return the head. + +**TC : O(2N), SC : O(N)** + +```cpp +Node* reverseList(Node* head) { + Node* temp = head; + stack st; + while(temp != nullptr){ + st.push(temp->data); + temp = temp->next; + } + temp = head; + while(temp != nullptr){ + temp->data = st.top(); + st.pop(); + temp = temp->next; + } + return head; +} +``` + +--- + +**Optimal Solution (Pointer swapping)** + +1. `Currrent` is a pointer to keep track of current nodes. Set it to head. `prev` is a pointer to keep track of previous nodes. Set it to NULL. `next` is a pointer to keep track of the next nodes. + +2. Set `next` to point next node to node pointed by current. + Change link between nodes pointed by `current` and `prev`. + Update `prev` to `current` and `current` pointer to `next`. + +- Perform STEP 2 until current reaches the end of the list. + +3. Set head as `prev`. This makes the head point at the last node. + +**TC : O(N) SC : O(1)** + +```cpp +Node* reverseList(Node* head) { + Node* prev = nullptr; + Node* current = head; + while(current != nullptr){ + Node* front = current->next; + current->next = prev; + prev = current; + current = front; + } + return prev; +} +```