File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // 19. Remove Nth Node From End of List
3
+ // https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
4
+ // Dale-Study
5
+ //
6
+ // Created by WhiteHyun on 2024/06/10.
7
+ //
8
+
9
+ /**
10
+ * Definition for singly-linked list.
11
+ * public class ListNode {
12
+ * public var val: Int
13
+ * public var next: ListNode?
14
+ * public init() { self.val = 0; self.next = nil; }
15
+ * public init(_ val: Int) { self.val = val; self.next = nil; }
16
+ * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
17
+ * }
18
+ */
19
+ class Solution {
20
+ func removeNthFromEnd( _ head: ListNode ? , _ n: Int ) -> ListNode ? {
21
+ let answerNode = ListNode ( 0 , head)
22
+ var footprintNode = head
23
+
24
+ for _ in 0 ..< n {
25
+ footprintNode = footprintNode? . next
26
+ }
27
+
28
+ var targetNode : ListNode ? = answerNode
29
+ while footprintNode != nil {
30
+ targetNode = targetNode? . next
31
+ footprintNode = footprintNode? . next
32
+ }
33
+ targetNode? . next = targetNode? . next? . next
34
+
35
+ return answerNode. next
36
+ }
37
+ }
You can’t perform that action at this time.
0 commit comments