My Swift Solution


  • 0
    J
    func reverseList(head: ListNode?) -> ListNode? {
            var currentNode = head
            var nextNode: ListNode?
            var preNode: ListNode?
            while currentNode != nil {
                nextNode = currentNode!.next
    
                currentNode!.next = preNode
                preNode = currentNode
    
                currentNode = nextNode
            }
    
            return preNode
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.