Short Java recursive solution


  • 0
    W
    public ListNode plusOne(ListNode head) {
        if(!helper(head)) return head;
        ListNode newHead = new ListNode(1);
        newHead.next = head;
        return newHead;
    }
    boolean helper(ListNode head) {
        return head.next == null || helper(head.next) ? ((head.val = (head.val+1)%10) == 0) : false;
    }
    

Log in to reply
 

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