Swift Recursive Solution


  • 0
    C
       class SolutionRecursive {
        func reverseList(head: ListNode?) -> ListNode? {
            if head == nil || head?.next == nil {
                return head
            }
            let leftList = head?.next
            let leftReverseList = reverseList(leftList)
    
            var cur = leftList
            while cur?.next != nil {
                cur = cur?.next
            }
            cur?.next = head
            head?.next = nil
    
            return leftReverseList
        }
     }

Log in to reply
 

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