Skip to content

Commit 3469daf

Browse files
committed
solve problem
1 parent a815df1 commit 3469daf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class ListNode {
2+
public var val: Int
3+
public var next: ListNode?
4+
public init() { self.val = 0; self.next = nil; }
5+
public init(_ val: Int) { self.val = val; self.next = nil; }
6+
public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
7+
}
8+
9+
class Solution {
10+
// Time complexity O(n)
11+
// Space complexity O(1)
12+
func reverseList(_ head: ListNode?) -> ListNode? {
13+
var reverseList: ListNode? = nil
14+
var currentHead = head
15+
while let node = currentHead {
16+
currentHead = node.next
17+
node.next = reverseList
18+
reverseList = node
19+
}
20+
return reverseList
21+
}
22+
}
23+

0 commit comments

Comments
 (0)