Skip to content

Commit 882e6e2

Browse files
committed
addTwoNumbers based on LinkedList
1 parent 9dffa18 commit 882e6e2

File tree

3 files changed

+287
-0
lines changed

3 files changed

+287
-0
lines changed

LinkedList.js

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* @Author: Z1hgq
3+
* @Date: 2019-09-16 14:18:35
4+
* @LastEditTime: 2019-09-17 14:08:17
5+
*/
6+
//单向链表构造函数
7+
const LinkedList = function() {
8+
/**
9+
* 单向链表中节点的构造函数
10+
* @param {Any} val 要传入链表的节点
11+
*/
12+
var Node = function(val) {
13+
this.val = val;
14+
this.next = null;
15+
}
16+
var length = 0;//单向链表的长度
17+
var head = null;//单向链表的头结点,初始化为NULL
18+
/**
19+
* 向单向链表尾部添加元素
20+
* @param {Any} val 要加入链表的节点
21+
*/
22+
this.append = function(val) {
23+
var node = new Node(val);
24+
var current;
25+
if (head == null) {
26+
head = node;
27+
} else {
28+
// 当前项等于链表头部元素.
29+
// while循环到最后一个,从而将该节点加入链表尾部。
30+
current = head;
31+
// 当next为null时,判定为false。退出循环。
32+
while (current.next) {
33+
current = current.next;
34+
}
35+
current.next = node;
36+
}
37+
length++;
38+
};
39+
40+
/**
41+
* 移除单向链表中某一个元素
42+
* @param {Number} position 要移除元素的位置
43+
* @return {Any} 移除成功返回被移除的元素,不成功则返回NULL
44+
*/
45+
this.removeAt = function(position) {
46+
if (position > -1 && position < length) {
47+
var current = head;
48+
var previous;
49+
var index = 0;
50+
51+
if (position == 0) {
52+
// 因为之前head指向第一个元素,现在把head修改为指向第二个元素。
53+
// 核心概念在于链表前后全靠指针链接,而非数组一般。
54+
// 所以只需要改变head的元素。
55+
head = current.next;
56+
} else {
57+
while (index++ < position) {
58+
// previous指要操作元素位置之前的那个元素,current表示之后的那个元素。
59+
previous = current;
60+
current = current.next;
61+
}
62+
63+
previous.next = current.next;
64+
}
65+
66+
length--;
67+
68+
return current.val;
69+
} else {
70+
return null;
71+
}
72+
};
73+
74+
/**
75+
* 向单向链表中插入某个元素
76+
* @param {Number} position 要插入的位置
77+
* @param {Any} val 要插入的元素
78+
* @return {Boolean} 插入成功返回true,失败返回false
79+
*/
80+
this.insert = function(position, val) {
81+
if (position >= 0 && position <= length) {
82+
var node = new Node(val);
83+
var current = head;
84+
var previous;
85+
var index = 0;
86+
87+
if (position == 0) {
88+
node.next = current;
89+
head = node;
90+
} else {
91+
while (index++ < position) {
92+
previous = current;
93+
current = current.next;
94+
}
95+
96+
previous.next = node;
97+
node.next = current;
98+
}
99+
100+
length++;
101+
return true;
102+
} else {
103+
return false;
104+
}
105+
};
106+
107+
/**
108+
* 将链表所有内容以字符串输出
109+
* @return {String} 要输出的字符串
110+
* current.val+',' 这边可以自行加 逗号或是 空格
111+
*/
112+
this.toString = function() {
113+
var current = head;
114+
var string = '';
115+
116+
while (current) {
117+
string += current.val+',';
118+
current = current.next;
119+
}
120+
return string;
121+
};
122+
123+
/**
124+
* 寻找某个元素在单向链表中的位置
125+
* @param {Any} val 要寻找的元素
126+
* @return {Number} 返回值>=0则代表找到相应位置
127+
*/
128+
this.indexOf = function(val) {
129+
var current = head;
130+
var index = 0;
131+
while (current) {
132+
if (val === current.val) {
133+
return index;
134+
}
135+
index++;
136+
current = current.next;
137+
}
138+
139+
return -1;
140+
};
141+
/**
142+
* 移除给定的元素
143+
* @param {Any} val 要移除的元素
144+
* @return {Number} 返回值>=0表示移除成功
145+
*/
146+
this.remove = function(val) {
147+
var index = this.indexOf(val);
148+
return this.removeAt(index);
149+
};
150+
151+
/**
152+
* 判断单向链表是否为空
153+
* @return {Boolean} 为空则返回true,不为空则返回false
154+
*/
155+
this.isAmpty = function() {
156+
return length === 0
157+
};
158+
159+
/**
160+
* 返回单向链表长度
161+
* @return {Number} 单向链表的长度
162+
*/
163+
this.size = function() {
164+
return length;
165+
};
166+
167+
/**
168+
* 获取单向链表的头部
169+
* @return {Any} 单向链表的头部
170+
*/
171+
this.getHead = function() {
172+
return head;
173+
}
174+
}

addTwoNumbers.js

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* @Author: Z1hgq
3+
* @Date: 2019-09-16 14:13:15
4+
* @LastEditTime: 2019-09-17 14:16:55
5+
*/
6+
/**
7+
* Definition for singly-linked list.
8+
* function ListNode(val) {
9+
* this.val = val;
10+
* this.next = null;
11+
* }
12+
*/
13+
/**
14+
* @param {ListNode} l1
15+
* @param {ListNode} l2
16+
* @return {ListNode}
17+
*/
18+
var addTwoNumbers = function(l1, l2) {
19+
// let arrl1 = [];
20+
// let arrl2 = [];
21+
// while(l1){
22+
// arrl1.push(l1.val);
23+
// l1 = l1.next;
24+
// }
25+
// while(l2){
26+
// arrl2.push(l2.val);
27+
// l2 = l2.next;
28+
// }
29+
// l1 = null;
30+
// l2 = null;
31+
// arrl1 = arrl1.reverse();
32+
// arrl2 = arrl2.reverse();
33+
// let strl1 = '';
34+
// let strl2 = '';
35+
// for(let num of arrl1){
36+
// strl1 += num;
37+
// }
38+
// for(let num of arrl2){
39+
// strl2 += num;
40+
// }
41+
// arrl1 = null;
42+
// arrl2 = null;
43+
let strl1 = '';
44+
let strl2 = '';
45+
while(l1){
46+
strl1 += l1.val;
47+
l1 = l1.next;
48+
}
49+
while(l2){
50+
strl2 += l2.val;
51+
l2 = l2.next;
52+
}
53+
strl1 = strl1.split('').reverse().join('');
54+
strl2 = strl2.split('').reverse().join('');
55+
//注意int有最大安全范围,超过这个范围的运算应使用BigInt,BigInt不受安全范围限制
56+
let res = (BigInt(strl1) + BigInt(strl2)).toString();
57+
strl1 = null;
58+
strl2 = null;
59+
let resNode = {
60+
val: res[res.length - 1],
61+
next: null,
62+
};
63+
//构造链表
64+
for(let i = res.length - 2; i >= 0; i--){
65+
let node = {
66+
val: res[i],
67+
next: null
68+
};
69+
let currentNode = resNode;
70+
while(currentNode.next){
71+
currentNode = currentNode.next;
72+
}
73+
currentNode.next = node;
74+
node = null;
75+
}
76+
return resNode;
77+
};
78+
79+
let l1 = {
80+
val: '1',
81+
next: {
82+
val: '2',
83+
next : {
84+
val: '3',
85+
next: null
86+
}
87+
}
88+
}
89+
let l2 = {
90+
val: '1',
91+
next: {
92+
val: '2',
93+
next : {
94+
val: '3',
95+
next: null
96+
}
97+
}
98+
}
99+
console.log(addTwoNumbers(l1,l2))
100+
101+
102+
103+
104+
105+
106+
107+
108+

twoSum.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
'''
2+
@Author: Z1hgq
3+
@Date: 2019-09-16 14:03:36
4+
@LastEditTime: 2019-09-16 14:03:36
5+
'''
16
def twoSumI(nums,target):
27
count = 0
38
res = []

0 commit comments

Comments
 (0)