very concise backtracking java solution


  • 0
    R
    public class Solution {
        public ListNode plusOne(ListNode head) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            plus(dummy);
            return dummy.val == 0 ? dummy.next : dummy;
        }
        
        private int plus(ListNode node) {
            int add = 0;
            if(node.next == null) add = 1;
            else add = plus(node.next);
            int ret = (node.val + add)/10;
            node.val = (node.val + add)%10;
            return ret;
        }
    }

Log in to reply
 

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