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:
5252
5353``` swift
5454class 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 }
5957 var count = 0
60- var current = head
61-
58+ var current = head
6259 while let currentNode = current{
6360 count += 1
6461 current = currentNode.next
65- }
66-
62+ }
6763 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 {
7970 current = current? .next
8071 return current
81- }else {
72+ } else {
8273 prev? .next = current? .next
8374 }
8475 break
85- }
86-
76+ }
8777 prev = current
88- current = current? .next
89-
90- }
91-
92- return head
93-
94-
78+ current = current? .next
79+ }
80+ return head
9581 }
9682}
9783```
You can’t perform that action at this time.
0 commit comments