1 Star 0 Fork 0

张志光/Leetcode-js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
2两数相加链表.html 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
zhangzhiguang123 提交于 2019-09-16 19:34 . 力扣
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var addTwoNumbers = function (l1, l2) {
function ListNode(val) {
this.val = val;
this.next = null;
}
function Llist() {
this.head = new ListNode("head");
this.find = find;
this.insert = insert;
}
function find(node) {
var targetNode = this.head;
while (1) {
if (targetNode.val === node) {
break;
}
targetNode = targetNode.next;
}
return targetNode;
}
function insert(newElement, node) {
var newNode = new ListNode(newElement);
var current = this.find(node);
newNode.next = current.next;
current.next = newNode;
}
var op;
if (l1.length < l2.length) {
op = l1;
l1 = l2;
l2 = op;
}
var l1List = new Llist();
var l2List = new Llist();
l1List.insert(l1[0], "head");
l2List.insert(l2[0], "head");
for (var i = 1; i < l1.length; i++) {
l1List.insert(l1[i], l1[i - 1]);
}
for (var i = 1; i < l2.length; i++) {
l2List.insert(l2[i], l2[i - 1]);
}
if (l1.length >= l2.length) {
var flag = 0;
var ppp = [];
var _o1 = l1List.head.next;
var _o2 = l2List.head.next;
var iIndex = 0;
var iIndex1 = 0;
while (_o1 != null) {
ppp[iIndex] = _o1.val;
iIndex++;
_o1 = _o1.next;
}
_o1 = l1List.head.next;
while (1) {
if (_o1 == null || _o2 == null) {
alert(ppp);
// return ppp;
break;
} else {
if (_o1.val + _o2.val >= 10) {
if (flag) {
ppp[iIndex1] = (_o1.val + _o2.val) % 10 + 1;
} else {
ppp[iIndex1] = (_o1.val + _o2.val) % 10;
}
flag = 1;
iIndex1++;
_o1 = _o1.next;
_o2 = _o2.next;
} else {
if (flag) {
ppp[iIndex1] = _o1.val + _o2.val + 1;
} else {
ppp[iIndex1] = _o1.val + _o2.val;
}
flag = 0;
iIndex1++;
_o1 = _o1.next;
_o2 = _o2.next;
}
}
}
}
};
addTwoNumbers([2,4,3], [5,6,4]);
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guangzz/Leetcode-js.git
git@gitee.com:guangzz/Leetcode-js.git
guangzz
Leetcode-js
Leetcode-js
master

搜索帮助