Skip to content

Commit 9916506

Browse files
committed
Improved task 19
1 parent 32d920f commit 9916506

File tree

1 file changed

+15
-29
lines changed
  • src/main/swift/g0001_0100/s0019_remove_nth_node_from_end_of_list

1 file changed

+15
-29
lines changed

src/main/swift/g0001_0100/s0019_remove_nth_node_from_end_of_list/readme.md

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,46 +52,32 @@ Here's the implementation:
5252

5353
```swift
5454
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}
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
```

0 commit comments

Comments
 (0)