/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
company/google/PlusOneLinkedList.java
50 строк
1 KB
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
// 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 PlusOneLinkedList { 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; } }