public class ListNode {
  public var val: Int
  public var next: ListNode?
  public init(_ val: Int) {
    self.val = val
    self.next = nil
  }
}

func reverseList(_ head: ListNode?) -> ListNode? {
  var curr = head
  var prev: ListNode?
  var next: ListNode?
  
  while curr != nil {
    next = curr?.next
    curr?.next = prev
    prev = curr
    curr = next
  }
  
  return prev
}

Resources

Leave a Reply