zoukankan      html  css  js  c++  java
  • Leetcode 2

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            ListNode *res = new ListNode(-1);
            ListNode *cur = res;
            int carry = 0;
            while (l1 || l2) {
                int n1 = l1 ? l1->val : 0;
                int n2 = l2 ? l2->val : 0;
                int sum = n1 + n2 + carry;
                carry = sum / 10;
                cur->next = new ListNode(sum % 10);
                cur = cur->next;
                if (l1) l1 = l1->next;
                if (l2) l2 = l2->next;
            }
            if (carry) cur->next = new ListNode(1);
            return res->next;
    
        }
    };

    我可能是个傻子,本来都给我逆序写好了,我偏要列表反转一下,mdzz

    既然写了也要贴出来

    ListNode* reverseList(ListNode *l){ 
            ListNode *res = l;
            ListNode *head = l->next;
            ListNode *cur = head;
            res->next = NULL;
                                           
            while(head){
                cur = head->next;
                head->next = res;
                res = head;
                head = cur;
            }
            return res;
        }

    ——

  • 相关阅读:
    幻灯片效果
    国外空间乱码的解决方法
    图片自动适应
    css圆角效
    iframe自适应兼容
    css圆角
    图片自动适应2
    JQuery实现智能输入提示(仿机票预订网站)
    AppDiag类
    c# 渐变算法
  • 原文地址:https://www.cnblogs.com/cunyusup/p/9598346.html
Copyright © 2011-2022 走看看