File tree Expand file tree Collapse file tree 1 file changed +15
-29
lines changed
src/main/swift/g0001_0100/s0019_remove_nth_node_from_end_of_list Expand file tree Collapse file tree 1 file changed +15
-29
lines changed Original file line number Diff line number Diff line change @@ -52,46 +52,32 @@ Here's the implementation:
52
52
53
53
``` swift
54
54
class Solution {
55
- func removeNthFromEnd (_ head : ListNode? , _ n : Int ) -> ListNode? {
56
-
57
- guard ((head? .next ) != nil ), n > 0 else {return nil }
58
-
55
+ func removeNthFromEnd (_ head : ListNode? , _ n : Int ) -> ListNode? {
56
+ guard ((head? .next ) != nil ), n > 0 else {return nil }
59
57
var count = 0
60
- var current = head
61
-
58
+ var current = head
62
59
while let currentNode = current{
63
60
count += 1
64
61
current = currentNode.next
65
- }
66
-
62
+ }
67
63
current = head
68
- count = count - n + 1
69
-
70
- var prev: ListNode?
71
-
72
- while let currentNode = current{
73
-
74
- count -= 1
75
-
76
- if count == 0 {
77
-
78
- if prev == nil {
64
+ count = count - n + 1
65
+ var prev: ListNode?
66
+ while let currentNode = current {
67
+ count -= 1
68
+ if count == 0 {
69
+ if prev == nil {
79
70
current = current? .next
80
71
return current
81
- }else {
72
+ } else {
82
73
prev? .next = current? .next
83
74
}
84
75
break
85
- }
86
-
76
+ }
87
77
prev = current
88
- current = current? .next
89
-
90
- }
91
-
92
- return head
93
-
94
-
78
+ current = current? .next
79
+ }
80
+ return head
95
81
}
96
82
}
97
83
```
You can’t perform that action at this time.
0 commit comments