[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; } }
- LeetCode 369. Plus One Linked List
- [leetCode] 369. Plus One Linked List
- [leetcode] 369. Plus One Linked List 解题报告
- LeetCode 369. Plus One Linked List(链表加1)
- Leetcode 369. Plus One Linked List (Medium) (cpp)
- [Leetcode] 369. Plus One Linked List 解题报告
- Plus One Linked List
- LeetCode: Plus One
- LeetCode Plus One
- [Leetcode] Plus One
- LeetCode: Plus One
- leetcode 33: Plus One
- [LeetCode] Plus One
- [leetCode] Plus One
- 【leetcode】Plus One
- [LeetCode]Plus One
- [leetcode]Plus One
- LeetCode-Plus One
- Qt中QGraphics类坐标映射关系详解(有图有真相,实例讲解)
- QT
- LNMP rm: cannot remove `.user.ini': Operation not permitted
- 抽象基类,虚函数,纯虚函数
- 0基础学PHP
- [leetCode] 369. Plus One Linked List
- KHL 006 作品-计算机-KM-小试-博客系统-功能记录
- 学习日记-world添加分割线
- KHL 007 转身-本科-编译原理
- 全面分析Spring的编程式事务及声明式事务管理
- 图像算法工程师招聘要求
- Spring-AOP 使用@AspectJ
- Effective STL学习笔记-条款1
- Effective STL学习笔记-条款7