• 两数相加——链表,leetcode第二题思路


    
    
    import org.w3c.dom.NodeList;
    
    public class Solution2 {
        public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dummyhead = new ListNode(0);
            ListNode p = l1, q = l2, curr = dummyhead;
            int addOne = 0;
            //考虑validation
            while (p == null || q == null || curr == null) {
                return null;
            }
            while (p != null || q != null) {
                int x = (p != null) ? p.val : 0;
                int y = (q != null) ? q.val : 0;
                int sum = addOne + x + y;
                addOne = sum / 10;
                curr.next = new ListNode(sum % 10);
                curr = curr.next;
                if (p != null) {
                    p = p.next;
                }
                if (q != null) {
                    q = q.next;
                }
                if (addOne > 0) {
                    curr.next = new ListNode(addOne);
                }
    
            }
            return dummyhead.next;
        }
    
    
        static class ListNode {
            int val;
            ListNode next;
    
            ListNode(int x) {
                val = x;
            }
        }
        public static String print(ListNode l){
            StringBuilder builder=new StringBuilder();
            while(l!=null){
                builder.append(l.val);
                l=l.next;
            }
    
            return builder.toString();
    
        }
    
        /**
         * @测试部分
         */
        public static void main(String args[]) {
            ListNode l1=new ListNode(2);
            ListNode l2=new ListNode(5);
            l1.next=new ListNode(4);
            l2.next=new ListNode(6);
            l1.next.next=new ListNode(3);
            l2.next.next=new ListNode(4);
            System.out.println(print(addTwoNumbers(l1,l2)));
        }
    }
    import org.w3c.dom.NodeList;

    public class Solution2 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummyhead = new ListNode(0);
    ListNode p = l1, q = l2, curr = dummyhead;
    int addOne = 0;
    //考虑validation
    while (p == null || q == null || curr == null) {
    return null;
    }
    while (p != null || q != null) {
    int x = (p != null) ? p.val : 0;
    int y = (q != null) ? q.val : 0;
    int sum = addOne + x + y;
    addOne = sum / 10;
    curr.next = new ListNode(sum % 10);
    curr = curr.next;
    if (p != null) {
    p = p.next;
    }
    if (q != null) {
    q = q.next;
    }
    if (addOne > 0) {
    curr.next = new ListNode(addOne);
    }

    }
    return dummyhead.next;
    }


    static class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
    val = x;
    }
    }
    public static String print(ListNode l){
    StringBuilder builder=new StringBuilder();
    while(l!=null){
    builder.append(l.val);
    l=l.next;
    }

    return builder.toString();

    }

    /**
    * @测试部分
    */
    public static void main(String args[]) {
    ListNode l1=new ListNode(2);
    ListNode l2=new ListNode(5);
    l1.next=new ListNode(4);
    l2.next=new ListNode(6);
    l1.next.next=new ListNode(3);
    l2.next.next=new ListNode(4);
    System.out.println(print(addTwoNumbers(l1,l2)));
    }
    }
  • 相关阅读:
    css属性及属性值
    Typora使用速记(常用的语法和快捷键)
    自己用的RGB对照表
    分享一波银行的面经攒一下人品
    使用hexo+github搭建博客(一)
    使用hexo+github搭建博客(二)配置和宠物系统
    vue3--相对于vue2的改变-T0档次
    java -- md5 加密
    spring boot--注解 案例
    java--整合druid
  • 原文地址:https://www.cnblogs.com/resort-033/p/13520448.html
Copyright © 2020-2023  润新知