We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a815df1 commit 3469dafCopy full SHA for 3469daf
reverse-linked-list/delight010.swift
@@ -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