Java recursion support any decimal system


  • 0
    E
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            add(l1,l2);
            format(l2,10);
            return l2;
            
        }
        
        private  ListNode add(ListNode l,ListNode res){
    
            if(l == null) {
                return res;
            }
            res.val += l.val;
            if(res.next==null && l.next!=null ) res.next = new ListNode(0);
            return add(l.next,res.next);
    
        }
    
        private  void format(ListNode node,int  index){
            if(node==null ) return;
    
            int other = node.val/index;
            int self = node.val%index;
    
            if(other>0) {
                if(node.next==null){
                    node.next = new ListNode(0);
                }
                node.next.val += other;
            }
            node.val = self;
            format(node.next,index);
        }
    

Log in to reply
 

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