You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md
+92-1Lines changed: 92 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,4 +31,95 @@ Given the `head` of a linked list, remove the <code>n<sup>th</sup></code> node f
31
31
*`0 <= Node.val <= 100`
32
32
*`1 <= n <= sz`
33
33
34
-
**Follow up:** Could you do this in one pass?
34
+
**Follow up:** Could you do this in one pass?
35
+
36
+
To solve the Remove Nth Node From End of List problem in Java with a `Solution` class, we'll follow these steps:
37
+
38
+
1. Define a `ListNode` class representing the nodes of the linked list.
39
+
2. Define a `Solution` class with a method named `removeNthFromEnd` that takes the head of the linked list and an integer `n` as input and returns the head of the modified list.
40
+
3. Create two pointers, `fast` and `slow`, and initialize them to point to the head of the list.
41
+
4. Move the `fast` pointer `n` steps forward in the list.
42
+
5. If the `fast` pointer reaches the end of the list (`fast == null`), it means that `n` is equal to the length of the list. In this case, remove the head node by returning `head.next`.
43
+
6. Move both `fast` and `slow` pointers simultaneously until the `fast` pointer reaches the end of the list.
44
+
7. At this point, the `slow` pointer will be pointing to the node just before the node to be removed.
45
+
8. Remove the `nth` node by updating the `next` reference of the node pointed to by the `slow` pointer to skip the `nth` node.
0 commit comments