4ms Recursive Golang soultion - O(n)


  • 1
    L
    func plusOne(head *ListNode) *ListNode {
    	if head != nil && add(head) == 1 {
    		return &ListNode{Val: 1, Next: head}
    	}
    
    	return head
    }
    
    func add(n *ListNode) int {
    	if n.Next == nil || add(n.Next) == 1 {
    		n.Val++
    	}
    
    	defer func() { n.Val %= 10 }()
    	return n.Val / 10
    }

Log in to reply
 

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