Skip to content

Commit

Permalink
LL Insertion-Deletion (#31)
Browse files Browse the repository at this point in the history
Co-authored-by: Rishabh <[email protected]>
  • Loading branch information
amitsuthar69 and Rishabh672003 authored Dec 5, 2023
1 parent 4cce54f commit 1c41afb
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 138 deletions.
138 changes: 0 additions & 138 deletions Data-Structures/Linear-Data-Structures/Linked-List/04-insertion.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## Delete Kth node of a Linked list

### Algorithm

1. Check if LL is empty.
2. Check if the `k`th node we've to delete is the head itslef.
3. Create a counter to keep a track of current position in LL.
4. Create a prev node to keep a sustain the `k-1`th node.
5. Iterate thorugh the LL and do the following steps :

- Increase counter to record current position.
- Bring the prev node to temp and move temp ahead.
- If current position is equal to k :
- Point the prev node to prev's next to next node (means skipping the `k`th node).
- Free the temp node (`k`th node) as it is no longer needed.
- Break the loop to avoid further iterations.

6. Return head of the LL

---

### Implementation

```cpp
Node* deleteK(Node* head, int k){
if (head == nullptr) return nullptr;
if (k == 1) { // if head node
Node* temp = head;
head = head->next;
delete temp;
return head;
}
int cnt = 0;
Node* temp = head;
Node* prev = nullptr;
while(temp != nullptr){
cnt++;
if (cnt == k) {
prev->next = prev->next->next;
free(temp);
break;
}
prev = temp;
temp = temp->next;
}
return head;
}
```
---
The step to create a temp node and then deleting it, is a memory saving option. The head could be deleted by just pointing current head to its next and then just returning the new head, but then we waste memory worth one node.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Insert a new Node at Head and Tail of a Linked List

### Algorithm

**Insertion at head**

1. Check if LL is empty, incase create new Node with new value.
2. Create a new node and assign the data to it.
3. Set the `next` pointer of the new node to the current head of the list.
4. Update the head of the list to the new node.

---

**Insert at tail**

1. Check if LL is empty, incase create new Node with new value.
2. Create a temp node, point it to head.
3. Iterate through the LL unless reach the tail node.
4. Create a new node, assign it the new value.
5. Make the tail node point the temp node.

---

### Implementation

```cpp
Node* insertAtHead(Node* head, int newVal){
if (head == nullptr) {
return new Node(val);
}
/* assuming the constructor only accepts
value and not pointer with it */
Node* temp = new Node(val);
temp->next = head;
temp = head;
}

Node* insertAtTail(Node* head, int newVal){
if (head == nullptr) {
return new Node(newVal);
}
Node* temp = head;
while (temp->next != nullptr){
temp = temp->next;
}
Node* newTail = new Node(newVal);
temp->next = newTail;
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Insert a new node kth position

### Algorithm

1. Check if LL is empty, incase create new Node with new value.
2. If k is equal to 1, use insertAtHead logic.
3. Create a counter to keep track of just behind node of `k`th node.
4. Create a temp node and point to head.
5. Traverse thorugh the LL and check the following :
- For each node, increase the counter and likewise move the temp ahead.
- If it is `k-1`th node, create new Node with new value.
- Point the new node's next to temp's next.
- Point temp's next to new node and break the loop.
6. Return head.

---

### Implementation

```cpp
Node* insertAtK(Node* head, int k, int newVal){
if (head == nullptr) {
if (k == 1) return new Node(newVal);
}

if (k == 1) {
Node* newNode = new Node(newVal);
newNode->next = head;
return newNode;
}

int cnt = 0;
Node* temp = head;
while (temp != nullptr){
cnt++;
if (cnt == (k-1)){
Node* x = new Node(newVal);
x->next = temp->next;
temp->next = x;
break;
}
temp = temp->next;
}
return head;
}
```

0 comments on commit 1c41afb

Please sign in to comment.