Skip to content

Commit 03b002d

Browse files
authored
Update README with solution for nth node removal
Added detailed explanation and implementation for removing the nth node from the end of a linked list.
1 parent 788a822 commit 03b002d

File tree

1 file changed

+92
-1
lines changed
  • src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list

1 file changed

+92
-1
lines changed

src/main/java/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,95 @@ Given the `head` of a linked list, remove the <code>n<sup>th</sup></code> node f
3131
* `0 <= Node.val <= 100`
3232
* `1 <= n <= sz`
3333

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.
46+
9. Return the head of the modified list.
47+
48+
Here's the implementation:
49+
50+
```java
51+
public class ListNode {
52+
int val;
53+
ListNode next;
54+
ListNode(int val) { this.val = val; }
55+
}
56+
57+
public class Solution {
58+
public ListNode removeNthFromEnd(ListNode head, int n) {
59+
ListNode dummy = new ListNode(0);
60+
dummy.next = head;
61+
ListNode fast = dummy;
62+
ListNode slow = dummy;
63+
64+
// Move the fast pointer n steps forward
65+
for (int i = 0; i <= n; i++) {
66+
fast = fast.next;
67+
}
68+
69+
// Move both pointers until the fast pointer reaches the end
70+
while (fast != null) {
71+
fast = fast.next;
72+
slow = slow.next;
73+
}
74+
75+
// Remove the nth node
76+
slow.next = slow.next.next;
77+
78+
return dummy.next;
79+
}
80+
81+
public static void main(String[] args) {
82+
Solution solution = new Solution();
83+
84+
// Example 1
85+
ListNode head1 = new ListNode(1);
86+
head1.next = new ListNode(2);
87+
head1.next.next = new ListNode(3);
88+
head1.next.next.next = new ListNode(4);
89+
head1.next.next.next.next = new ListNode(5);
90+
int n1 = 2;
91+
ListNode result1 = solution.removeNthFromEnd(head1, n1);
92+
printList(result1); // Output: [1,2,3,5]
93+
94+
// Example 2
95+
ListNode head2 = new ListNode(1);
96+
int n2 = 1;
97+
ListNode result2 = solution.removeNthFromEnd(head2, n2);
98+
printList(result2); // Output: []
99+
100+
// Example 3
101+
ListNode head3 = new ListNode(1);
102+
head3.next = new ListNode(2);
103+
int n3 = 1;
104+
ListNode result3 = solution.removeNthFromEnd(head3, n3);
105+
printList(result3); // Output: [1]
106+
}
107+
108+
private static void printList(ListNode head) {
109+
if (head == null) {
110+
System.out.println("[]");
111+
return;
112+
}
113+
StringBuilder sb = new StringBuilder("[");
114+
while (head != null) {
115+
sb.append(head.val).append(",");
116+
head = head.next;
117+
}
118+
sb.setLength(sb.length() - 1);
119+
sb.append("]");
120+
System.out.println(sb.toString());
121+
}
122+
}
123+
```
124+
125+
This implementation provides a solution to the Remove Nth Node From End of List problem in Java.

0 commit comments

Comments
 (0)