Skip to content

Commit d7fe340

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
Create 0021.js
1 parent 4bdcd73 commit d7fe340

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

0021-merge-two-sorted-lists.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,31 @@ var mergeTwoLists = function(l1, l2) {
3333

3434
return l1;
3535
};
36+
37+
// var mergeTwoLists = function(list1, list2) {
38+
// const dummy = new LinkedListNode();
39+
// current = dummy
40+
41+
// p1 = list1
42+
// p2 = list2
43+
44+
// while (p1 && p2) {
45+
// if (p1.val < p2.val) {
46+
// current.next = p1
47+
// p1 = p1.next
48+
// } else {
49+
// current.next = p2
50+
// p2 = p2.next
51+
// }
52+
53+
// current = current.next
54+
// }
55+
56+
// if (p1) {
57+
// current.next = p1
58+
// } else {
59+
// current.next = p2
60+
// }
61+
62+
// return dummy.next
63+
// };

example/3.Linkedlist/0021.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function(list1, list2) {
14+
15+
};
16+
17+
const myLinkedList = new LinkedList();
18+
myLinkedList.prepend(1);
19+
myLinkedList.append(2);
20+
myLinkedList.append(4);
21+
22+
const myLinkedList2 = new LinkedList();
23+
myLinkedList2.prepend(1);
24+
myLinkedList2.append(3);
25+
myLinkedList2.append(4);
26+
27+
const myLinkedList3 = new LinkedList();
28+
29+
const myLinkedList4 = new LinkedList();
30+
31+
const myLinkedList5 = new LinkedList();
32+
33+
const myLinkedList6 = new LinkedList();
34+
myLinkedList6.prepend(0);
35+
36+
const executeList = (list, list2) => mergeTwoLists(list.head, list2.head)
37+
38+
const execute = (list, list2) => {
39+
console.log('travers')
40+
console.log(list.toString());
41+
console.log(list2.toString());
42+
const list1 = executeList(list, list2)
43+
console.log(traversList(list1))
44+
}
45+
46+
execute(myLinkedList, myLinkedList2) // 1,2,4 1,3,4 // 1,1,2,3,4,4
47+
execute(myLinkedList3, myLinkedList4) // [] [] // []
48+
execute(myLinkedList5, myLinkedList6) // [] [0] // [0]
49+
50+

0 commit comments

Comments
 (0)