zoukankan      html  css  js  c++  java
  • LeetCode OJ:Add Two Numbers (相加链表之数)

    You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    将两个链表上的数相加就可以,大于10进一位,注意下相加时候的细节就可以了,我这里

    吧prev节点记录下来,这样最后多生成节点的时候便于将最后一个多余的节点删除:

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     ListNode *next;
     6  *     ListNode(int x) : val(x), next(NULL) {}
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    12         ListNode * curr = new ListNode(0);
    13         ListNode * root = curr;
    14         ListNode * prev = curr;
    15         int currVal = 0;
    16         while(l1 != NULL && l2 != NULL){
    17             currVal = l1->val + l2->val;
    18             curr->val += currVal;
    19             curr->next = new ListNode(curr->val/10);
    20             curr->val%=10;
    21             prev = curr;
    22             curr = curr->next;
    23             l1 = l1->next;
    24             l2 = l2->next;
    25         }
    26         while(l1 != NULL){
    27             curr->val += l1->val;
    28             curr->next = new ListNode(curr->val/10);
    29             curr->val %= 10;
    30             prev = curr;
    31             curr = curr->next;
    32             l1 = l1->next;
    33         }
    34         while(l2 != NULL){
    35             curr->val += l2->val;
    36             curr->next = new ListNode(curr->val/10);
    37             curr->val %= 10;
    38             prev = curr;
    39             curr = curr->next;
    40             l2 = l2->next;
    41         }
    42         if(curr->val == 0){
    43             prev->next = NULL;
    44             delete curr;
    45         }
    46         return root;
    47     }
    48 };

    感觉写的有点麻烦,应该有很多的重复代码可以改正,但是我暂时找不出来了,先这样吧。

    更新下,以前脑子抽了写出了那样的代码。 其实三个while循环都可以放到一个while中, 下面用java在写一起,方法还是类似的:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    11         ListNode tmp = new ListNode(0);
    12         ListNode head = tmp;
    13         int carry = 0;
    14         while(l1!=null || l2!=null || carry != 0){
    15             int val = ((l1 != null)?l1.val:0) + ((l2!=null)?l2.val:0) + carry;
    16             carry = carry/10 + val/10;
    17             val %= 10;
    18             tmp.next = new ListNode(val);
    19             tmp = tmp.next;
    20             l1 = l1!=null ? l1.next : l1;
    21             l2 = l2!=null ? l2.next : l2;
    22         }
    23         return head.next;
    24     }
    25 }
  • 相关阅读:
    js json string 互转
    更新内置flash方法[转]
    CSS设置滚动条样式[转]
    ArcGIS JavaScript API本地部署离线开发环境[转]
    正则匹配整数和小数
    把数字字符串转换为日期
    获取滚动条高度的兼容问题
    angular实现多个div的展开和折叠
    IE浏览器overflow:srcoll兼容问题记录
    input验证,光标左右移动问题
  • 原文地址:https://www.cnblogs.com/-wang-cheng/p/4869650.html
Copyright © 2011-2022 走看看