# Delete Kth node From End

Posted: 22 Oct, 2020

Difficulty: Moderate

#### You have been given a singly Linked List of 'N' nodes with integer data and an integer 'K'. Your task is to remove the Kth node from the end of the given Linked List.

##### For example:

```
The given linked list is 1 -> 2 -> 3 -> 4-> 'NULL'. and 'K' is 2
After removing the second node from the end, the linked list become 1->2->4->'NULL'
```

##### Follow Up:

```
Can you solve this without finding the length of the linked list and using O(1) extra space?
```

##### Input format :

```
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.
The first line of each test case contains a single integer 'K', representing the index(1 based indexing) of a node from the last to be deleted.
The second line of each test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
```

##### Output format :

```
For each test case, print a single line that contains the updated linked list in a linear fashion. A single space will separate all the list data and -1 will indicate the end of the list.
Print output of each test case in a separate line.
```

##### Note :

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

##### Constraints :

```
1 <= 'T' <= 5
0 <= 'N' <= 10 ^ 5
0 <= 'K' <= 'N'
1 <= 'DATA' <= 10 ^ 9 and 'DATA' != -1
Time Limit: 1 sec.
```

Approach 1

The naive solution is to process all the nodes from the front side of the linked list and keep adding a node to the list. Now we can easily remove the Kth node from the end of the list by simply replacing the next pointer of the ('LENGTH' - 'K' - 1)th node (0-based indexing from start) of the list with the ('LENGTH' - 'K' + 1)th node. This way we can remove the Kth node from the end of the linked list.

Approach 2

- Find the length of Linked List 'L'
- Check if 'L' = 'K' then remove the head by assigning head to head’s next node.
- Else start iterating through the linked list until it comes to ('L' - 'K' -1 )th(0 based indexing from start) node. We will remove the ('L' - 'K')th node by relinking the next pointer of ('L' - 'K' - 1)th node to the ('L' - 'K' + 1)th node.

Approach 3

We can remove the required node without finding the length of the given linked list by using two pointers 'SLOW' and 'FAST', which are 'K' nodes apart from each other.

- Initially, the 'FAST' pointer advances the list by 'K' nodes from the beginning and the 'SLOW' is a pointer to the head of the linked list.
- Now both pointers are exactly separated by 'K' distance from each other. We will maintain a constant gap by advancing both pointers together until the 'FAST' pointer reaches the last node.
- When the 'FAST' pointer is at the last node then the 'SLOW' pointer will be at ('K' + 1)th node from the end of the linked list.
- At last, we will set the next of 'SLOW' pointer to its next of next node.

SIMILAR PROBLEMS

# Replace The Linked List

Posted: 1 Apr, 2021

Difficulty: Moderate

# Implement a Queue

Posted: 27 Jul, 2021

Difficulty: Easy

# Vertical Sum in BST

Posted: 27 Jul, 2021

Difficulty: Moderate

# Remove Duplicates From Sorted List

Posted: 21 Sep, 2021

Difficulty: Easy

# Binary Linked List To Integer

Posted: 22 Sep, 2021

Difficulty: Easy