publicclassSolution{ public ListNode plusOne(ListNode head){ if (plusOneHelper(head) == 0) { return head; } //need addtional node ListNode newHead = new ListNode(1); newHead.next = head; return newHead; } // plus one for the rest of the list starting from node and return carry //because last node.next is null, let null return 1 and it is equivalent to "plus one" to the least significant digit
privateintplusOneHelper(ListNode node){ if (node == null) { return1; } int sum = node.val + plusOneHelper(node.next); node.val = sum % 10; return sum / 10; } }