“This is the 14th day of my participation in the First Challenge 2022.
Leetcode-19 Removes the NTH node from the list
Subject to introduce
Given a linked list, delete the NTH node from the bottom of the list and return the head of the list.
Example 1
Enter: head = [1.2.3.4.5], n = 2Output:1.2.3.5]
Copy the code
Example 2
Enter: head = [1], n = 1Output: []Copy the code
Example 3
Enter: head = [1.2], n = 1Output:1]
Copy the code
Tip:
- The number of nodes in the linked list is zero
sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Their thinking
Idea 1: Double traversal
Double traversal refers to traversing the list twice. The first traversal of the whole list can obtain the length of the whole list. Then, to obtain the penultimate NTH node, we only need to traverse the list from the beginning, and then go forward the length of the list -n steps to reach the last node of the node to be deleted. You can delete the NTH node from the penultimate list by pointing the next pointer to the node to be deleted
The problem solving steps
- Define a virtual head node
newNode
(in case the node to be deleted is the head node of the linked list), the virtual head nodenext
Point to the actual head node - define
p
The pointer traverses from the virtual head node,k
Used to record the length of a linked list - if
p.next
Not empty, willp
Point to thep.next
And thenk++
- After recording the list length in step 3, the
p
Pointer repoints to the virtual head node of the linked listnewNode
And thenp
The needle goes forwardk - n
Step to the last node of the node to be deleted - will
p
Pointer to a nodenext
Pointer top.next.next
To delete the node to be deleted - Returns the next node of the virtual head node
The problem solving code
var removeNthFromEnd = function(head, n) {
// Define a virtual head node
const newNode = new ListNode(-1, head)
// Define a pointer to the virtual head node
let p = newNode, k = 0
// Get the length of the list
while (p.next) {
p = p.next
k++
}
// the p pointer points to the virtual head node
p = newNode
// The distance from the p node to the node before the deletion
let step = k - n
// Go to the node before the deleted node
while (step--) {
p = p.next
}
// Delete the penultimate node
p.next = p.next.next
// return the head node
return newNode.next
};
Copy the code
Idea 2: double pointer method
Two Pointers can be used. The first pointer takes n steps before the second pointer, and then the two Pointers move forward at the same time. When the fast pointer reaches the end of the list, the node that the slow pointer points to is the last node of the node to be deleted
The problem solving steps
- Define a virtual head node
newNode
(in case the node to be deleted is the head node of the linked list), the virtual head nodenext
Point to the actual head node - Defining fast Pointers
next
And slow pointerpre
Also start with the virtual head node - Quick pointer
next
To go forwardn
步 - The fast and slow hands move forward at the same time until the fast hands
next
Go to the end of the list - will
pre
Of the node pointing tonext
Pointer topre.next.next
- Returns the next node of the virtual head node
The problem solving code
var removeNthFromEnd = function(head, n) {
// Create a virtual head node
const newNode = new ListNode(-1, head)
// Define two Pointers to the virtual head node
let pre = next = newNode
// The fast pointer moves forward n positions
while (n--) {
next = next.next
}
// The fast pointer and the slow pointer move at the same time. When the fast pointer reaches the end, the slow pointer points to the node before the deleted node
while (next.next) {
pre = pre.next
next = next.next
}
// Set the next node of the slow pointer to the next node of the deleted node
pre.next = pre.next.next
// return the head node
return newNode.next
};
Copy the code
So far, we have removed the NTH node from the list in two ways