Java solution


  • 0
    M
    public ListNode plusOne(ListNode head) {
        ListNode node = head;
        ListNode non9 = null;
        while(node != null) {
            if(node.val != 9)
                non9 = node;
            node = node.next;    
        }
        if(non9 == null) {
            non9 = new ListNode(1);
            non9.next = head;
            head = non9;
        } else {
            non9.val++;
        }
        node = non9.next;
        while(node != null) {
            node.val = 0;
            node = node.next;
        }
        return head;
    }

Log in to reply
 

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