Skip to content

Commit 766b248

Browse files
committed
feat: Add solution for LeetCode problem 19
1 parent 91671e5 commit 766b248

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

0 commit comments

Comments
 (0)