Swift 2 Solution: Please Critique


  • 0
    T

    I've seen much briefer solutions, although my structures do have some more fluff for convenience and output formatting. The code is Playground friendly. Where can I improve?

    class LinkedList: CustomStringConvertible
    {
        var first: LinkedListNode?
    
        init(var number: Int)
        {
            number = max(0, number)
    
            var previousNode = first
    
            for digit in String(number).characters.reverse()
            {
                let newNode = LinkedListNode(data: Int("\(digit)")!)
    
                if let previous = previousNode
                {
                    previous.next = newNode
                    previousNode = newNode
                }
                else
                {
                    first = newNode
                    previousNode = newNode
                }
            }
        }
    
        func iterate(operation: Int -> Void)
        {
            var currentNode = first
            while let node = currentNode
            {
                operation(node.data)
                currentNode = node.next
            }
        }
    
        var description: String
        {
            var output = ""
            var currentNode = first
    
            while currentNode != nil
            {
                output += "\(currentNode!.data)"
                currentNode = currentNode!.next
                if let _ = currentNode
                {
                    output += " -> "
                }
            }
            return output
        }
    }
    
    class LinkedListNode: CustomStringConvertible
    {
        var data = 0
        var next: LinkedListNode?
    
        init(data: Int)
        {
            self.data = data
        }
    
        var description: String
        {
            return "\(data)"
        }
    }
    
    func addList(list1: LinkedList, toList list2: LinkedList) -> LinkedList?
    {
        return addTwoNumbers(list1, list2: list2)
    }
    
    func addTwoNumbers(list1: LinkedList, list2: LinkedList) -> LinkedList?
    {
        var list1Digits = [Int]()
        var list2Digits = [Int]()
    
        list1.iterate
        {
            digit in
            list1Digits.append(digit)
        }
    
        list2.iterate
        {
            digit in
            list2Digits.append(digit)
        }
    
        func stringFromReversedList(list: [Int]) -> String
        {
            var output = ""
            for digit in list.reverse()
            {
                output += "\(digit)"
            }
            return output
        }
    
        let l1String = stringFromReversedList(list1Digits)
        let l2String = stringFromReversedList(list2Digits)
    
        if let a = Int(l1String), let b = Int(l2String)
        {
            return LinkedList(number: a + b)
        }
        else
        {
            return nil
        }
    }
    
    let l1 = LinkedList(number: 342)
    let l2 = LinkedList(number: 465)
    
    addTwoNumbers(l1, list2: l2) // 7 -> 0 -> 8
    
    addTwoNumbers(LinkedList(number: 1), list2: LinkedList(number: 20000)) // 1 -> 0 -> 0 -> 0 -> 2
    
    addList(LinkedList(number: 25), toList: LinkedList(number: 1023)) // 8 -> 4 -> 0 -> 1

Log in to reply
 

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