java面试题(5)

来源:互联网 发布:oracle sql 去掉重复 编辑:程序博客网 时间:2024/06/05 19:22

原题:

// Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.// You may assume the integer do not contain any leading zero, except the number 0 itself.// The digits are stored such that the most significant digit is at the head of the list.// Example:// Input:// 1->2->3// Output:// 1->2->4/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */

答案:

public class Solution {    public ListNode plusOne(ListNode head) {        if(plusOneRecursive(head) == 0) {            return head;        }        else {            ListNode newHead = new ListNode(1);            newHead.next = head;            return newHead;        }    }    private int plusOneRecursive(ListNode head) {        if(head == null) return 1;        int carry = plusOneRecursive(head.next);        if(carry == 0) return 0;        int value = head.val + 1;        head.val = value % 10;        return value/10;    }}
原创粉丝点击