[leetCode] 369. Plus One Linked List

来源:互联网 发布:手机照片阅读软件 编辑:程序博客网 时间:2024/06/05 23:57

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->3Output: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;          } }


原创粉丝点击