1、2两数相加 - 力扣(LeetCode)
思路:
- 只要有任意一个链表还没有为空的时候就继续加,当链表为空的时候但是t不尾0,还是进入循环进行操作
代码:
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode cur1 = l1;ListNode cur2 = l2;ListNode head = new ListNode(0);//虚拟头结点ListNode pre = head;//虚拟尾节点 int t = 0;//判断条件while(cur1 != null || cur2 != null || t != 0){if(cur1 != null){t += cur1.val;cur1 = cur1.next;}if(cur2 != null){t += cur2.val;cur2 = cur2.next;}pre.next = new ListNode(t % 10);pre = pre.next;t /= 10;}return head.next;}
2、