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 883f5b8 commit 6c25a20Copy full SHA for 6c25a20
src/com/gatsby/offerII/_21RemoveNthFromEnd.java
@@ -0,0 +1,34 @@
1
+package com.gatsby.offerII;
2
+
3
+import com.gatsby.ListNode;
4
5
+/**
6
+ * @ClassName: _21RemoveNthFromEnd
7
+ * @Description: 剑指 Offer II 021. 删除链表的倒数第 n 个结点
8
+ * <p>
9
+ * 1 -> 2 -> 3 -> 4 -> 5
10
+ * @author: Gatsby
11
+ * @date: 2022/7/14 13:38
12
+ */
13
14
+public class _21RemoveNthFromEnd {
15
+ public ListNode removeNthFromEnd(ListNode head, int n) {
16
+ ListNode dummy = new ListNode(-1);
17
+ dummy.next = head;
18
+ ListNode fast = dummy;
19
+ ListNode slow = dummy;
20
+ while (n-- != 0) {
21
+ fast = fast.next;
22
+ }
23
24
+ while (fast != null && fast.next != null) {
25
26
+ slow = slow.next;
27
28
29
+ slow.next = slow.next.next;
30
+ return dummy.next;
31
32
+}
33
34
0 commit comments