Java日记2018-07-20

一 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

package com.lyc.letcode;



public class Solution0720 {
    public static ListNode add2number(ListNode n1,ListNode n2){
        if (n1==null){
            return n2;
        } 
        if(n2==null) return n1;
        return add2numCore(n1,n2,0);
    }
    public static ListNode add2numCore(ListNode l1,ListNode l2,int carry){
        if(l1==null && l2==null && carry==0) return null;
        ListNode res = new ListNode(carry);
        if(l1!=null) res.val+=l1.val;
        if(l2!=null) res.val+= l2.val;
        res.next = add2numCore(l1.next,l2.next,res.val/10);
        res.val = res.val%10;
        return res;
        
    }
    
    public static void  main(String[] args){
        ListNode l1 = new ListNode(2);
        ListNode l2 = new ListNode(4);
        ListNode l3 = new ListNode(3);
        ListNode l5 = new ListNode(5);
        ListNode l6 = new ListNode(6);
        ListNode l7 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l5.next = l6;
        l6.next = l7;
        
        ListNode rt = add2number(l1,l5);
        System.out.println(rt.next.next.val);
    }

}

二 Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced
BST.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,136评论 0 10
  • 总觉得现在的工作薪水太少,想搞点副业。一合计,发现自己也就懂点诗词皮毛,能聊聊时下政治,编程、流行、搞销售啥的都不...
    立而后行阅读 1,371评论 0 0
  • words: They set off a road trip across the country set of...
    Pan11阅读 1,085评论 0 0
  • 晚风拂柳月无声 灯火通明映新城 孤鸟抱水欲入梦 小河潺潺水叮叮 不知暗香何处来 邀影对月共徘徊 更深夜静人儿空 最...
    夜空北辰阅读 1,638评论 0 0
  • 花了一天时间做的彩虹瓶,唔,大部分的时间都是在泡海洋宝宝。 最后材料比较多就用自己喝完的酒瓶子装了两个。可以在家里...
    橘温暖茶阅读 3,258评论 2 5