Java Recursive Solution


  • 0
    public class Solution {
        public ListNode plusOne(ListNode head) {
            if( head == null ) return new ListNode(1);
            ListNode dummy = new ListNode(1);
            dummy.next = head;
            return ( helper( head ) > 0 ? dummy : head);
        }
        
        public int helper( ListNode node ) {
            if( node == null ) return 1;
            int sum = node.val + helper( node.next );
            node.val = sum%10;
            return sum/10;
        }
    }
    

Log in to reply
 

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