国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Leetcode Linked List Problem 鏈表問題合集

2019-11-10 22:31:24
字體:
來源:轉載
供稿:網友

1. Leet Code OJ 2. Add Two Numbers

You are given two non-empty linked lists rePResenting two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

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

您將獲得兩個非空鏈接列表,表示兩個非負整數。 數字以相反的順序存儲,并且它們的每個節點包含單個數字。 添加兩個數字并將其作為鏈接列表返回。

您可以假定這兩個數字不包含任何前導零,除了數字0本身。

輸入:(2→4→3)+(5→6→4) 輸出:7 - > 0 - > 8

代碼:

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode node = new ListNode(0); if(l1 == null && l2 == null) return l1; back(node, l1, l2); return node; } public void back(ListNode result, ListNode l1, ListNode l2){ if(l1 != null) result.val += l1.val; else l1 = new ListNode(0); if(l2 != null) result.val += l2.val; else l2 = new ListNode(0); ListNode node = new ListNode(0); if(result.val >= 10){//說明會下一個節點值至少為1 result.val = result.val % 10; node.val = 1; result.next = node; } if( (l1.next != null || l2.next != null)){ result.next = node; back(result.next, l1.next, l2.next); } }}
上一篇:vi

下一篇:webapi的使用

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石嘴山市| 溧阳市| 崇左市| 清流县| 左云县| 喀喇沁旗| 盐亭县| 六盘水市| 界首市| 诸城市| 修文县| 会东县| 扶风县| 辰溪县| 庆安县| 琼海市| 丹凤县| 阿拉善右旗| 吉林省| 永丰县| 腾冲县| 靖远县| 确山县| 武冈市| 郁南县| 师宗县| 都江堰市| 类乌齐县| 社会| 房山区| 荃湾区| 马尔康县| 鄄城县| 绥中县| 秦安县| 东光县| 大方县| 紫金县| 大丰市| 本溪市| 肃南|