-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_remove_nth_node.swift
38 lines (37 loc) · 1.02 KB
/
19_remove_nth_node.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func removeNthFromEnd(_ head: ListNode?, _ num: Int) -> ListNode? {
guard let h = head else { return nil }
var n = h
var c = 1
while n.next != nil {
c += 1
n = n.next!
}
let target = c - num + 1
if target == 1 { return h.next }
n = h
var count = 1
while n.next != nil {
count += 1
if count == target && count <= c-1 {
n.next = n.next!.next!
return h
} else if count == target {
n.next = nil
return h
}
n = n.next!
}
return h
}
}