Given a non-negative number represented as a singly linked list of digits, plus one to the number.

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 (head == null)
return null;
int carry = helper(head);
if (carry == ) {
ListNode newHead = new ListNode();
newHead.next = head;
return newHead;
} else {
return head;
}
} public int helper(ListNode head) {
if (head == null)
return ; int carry = helper(head.next);
int value = head.val;
head.val = (value + carry) % ;
carry = (value + carry) / ;
return carry;
}
}

方法二:

先reverse, 加一,再reverse.

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
private ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
ListNode next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
} public ListNode plusOne(ListNode head) { head = reverse(head);
ListNode newHead = head;
if (head == null)
return null;
int carry = ; ListNode prev = null;
while (head != null) {
int value = head.val;
head.val = (value + carry) % ;
carry = (value + carry) / ;
prev = head;
head = head.next;
} if (carry == ) {
ListNode end = new ListNode();
prev.next = end;
} return reverse(newHead);
}
}

Plus One Linked List的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

随机推荐

  1. python 2.7的安装

    最近准备入手学习python 这里我是按照:http://blog.csdn.net/jcjc918/article/details/11022345 来的 我在安装python 3 的时候发现上下左 ...

  2. JS模式-基本的单例模式

    //singleton var SingletonTester = (function(){ function Singleton(options){ options = options || {}; ...

  3. poj2406 KMP

    kmp简单题 找循环节.由于KMP的next[]数组,所以可以考虑最后一组的情况,及next[n]的值:n-next[n]的值表示一个循环节. 如果n%(n-next[n])!=0表明该循环不成立.不 ...

  4. BZOJ4195 程序自动分析

    Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...

  5. 微信支付(APP)集成时碰到的问题(.net提示“无权限”、iOS跳转到微信支付页面中间只有一个“确定”按钮)

    直入主题之前,请容我吐槽一下微*的官方东西:ASDFQ%#$%$#$%^FG@#$%DSFQ#$%.......:吐槽玩了!大家心照就好. 要完成手机APP跳转到微信的APP进行微信支付,需要进行如下 ...

  6. bzoj2096 pilots

    依旧是维护两个单调队列,只是队首检查的方式略有变动 /*by SilverN*/ #include<iostream> #include<algorithm> #include ...

  7. java时间库Joda-Time

    虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...

  8. hihocoder #1270 建造基地

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...

  9. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  10. CSS------添加注释框

    my.html <div><a href="#" class="tip">你好<span><k>哈哈哈哈< ...