[leetCode] 369. Plus One Linked List

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

方法一

思路:题意是链表加1,就是在链表最后一个node.val+1,因此要处理加1之后的carry情况,还可能会有新的一位出现

例如:第一步:1   ->   2   ->   3 经过reverse

         第二步:dummy   ->   3   ->   2   ->   1

                        pre           cur

         第三步:  再把最后的结果reverse

过程:while循环最后到cur是1这个位置,满足cur!=null,然后cur=cur.next,因此最后cur为null,而pre在cur之前一结点是1这个位置

         因此如果有新的一位出现,就new在pre.next

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {                                                
    public ListNode plusOne(ListNode head) {
        if(head==null){
            return null;
        }
        
        ListNode reverseHead=reverse(head);
        ListNode dummy=new ListNode(-1);
        dummy.next=reverseHead;
        ListNode pre=dummy;
        ListNode cur=reverseHead;
        int carry=1;
        
        while(cur!=null){
            int sum=cur.val+carry;
            cur.val=sum%10;
            carry=sum/10;
            pre=pre.next;
            cur=cur.next;
        }
        if(carry!=0){
           pre.next=new ListNode(carry); 
        }      
        return reverse(reverseHead);
    }
    
    private ListNode reverse(ListNode head){
        ListNode pre=null;
        while(head!=null){
            ListNode temp=head.next;
            head.next=pre;
            pre=head;
            head=temp;
        }
        return pre;
    }
}

方法二

思路:从stack拿到ListNode经过carry这边的计算,不断向ListNode前面添加

过程:                                                               3    ->  null  (rightOne)

                                                          2   ->   rigthOne    (这个时候3这个result赋值成了rightOne)

                                          1  ->   rigthOne            

                                     rigthOne(如果有carry就继续向前面添加)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode plusOne(ListNode head) {
        if(head==null){
            return null;
        }
        Stack<ListNode> stack=new Stack<>();
        while(head!=null){
            stack.push(head);
            head=head.next;
        }
        
        ListNode result=null;
        ListNode rightOne=null;
        int carry=1;
        while(!stack.isEmpty()){
            ListNode cur=stack.pop();
            int sum=carry+cur.val;
            result=new ListNode(sum%10);
            carry=sum/10; 
            result.next=rightOne;                         //result的下一个指向rightOne
            rightOne=result;                              //rightOne移到result的位置       
        }
        
        if(carry!=0){
            result=new ListNode(carry);  
            result.next = rightOne;
        }
        return result;      
    } 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值