We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5f1ed68 commit 612595aCopy full SHA for 612595a
remove-nth-node-from-end-of-list/TonyKim9401.java
@@ -0,0 +1,21 @@
1
+// TC: O(n)
2
+// Visit all elements in the worst case
3
+// SC: O(1)
4
+// Keep using ready assigned variables only
5
+class Solution {
6
+ public ListNode removeNthFromEnd(ListNode head, int n) {
7
+ ListNode output = new ListNode(0, head);
8
+ ListNode dummy = output;
9
+
10
+ for (int i = 0; i < n; i++) head = head.next;
11
12
+ while (head != null) {
13
+ head = head.next;
14
+ dummy = dummy.next;
15
+ }
16
17
+ dummy.next = dummy.next.next;
18
19
+ return output.next;
20
21
+}
0 commit comments