Skip to content

Commit 058d082

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
add linked lists
1 parent 6b2b68c commit 058d082

File tree

8 files changed

+326
-2
lines changed

8 files changed

+326
-2
lines changed

0002-add-two-numbers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,25 @@ var addTwoNumbers = function(l1, l2) {
3636

3737
return result.next;
3838
};
39+
40+
// var addTwoNumbers = function(l1, l2) {
41+
// const dummy = new LinkedListNode();
42+
// let cur = dummy;
43+
44+
// let carry = 0;
45+
// while (l1 || l2 || carry) {
46+
// const v1 = l1 ? l1.val : 0;
47+
// const v2 = l2 ? l2.val : 0;
48+
49+
// let val = v1 + v2 + carry;
50+
// carry = Math.floor(val / 10);
51+
// val = val % 10;
52+
// cur.next = new LinkedListNode(val);
53+
54+
// cur = cur.next;
55+
// l1 = l1 ? l1.next : null;
56+
// l2 = l2 ? l2.next : null;
57+
// }
58+
59+
// return dummy.next;
60+
// };

0143-reorder-list.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,27 @@ var reorderList = function(head) {
5555
list2 = center1.next;
5656
}
5757
};
58+
59+
// var reorderList = function(head) {
60+
// if (!head) return;
61+
62+
// const nodes = [];
63+
// let cur = head;
64+
// while (cur) {
65+
// nodes.push(cur);
66+
// cur = cur.next;
67+
// }
68+
69+
// let i = 0, j = nodes.length - 1;
70+
// while (i < j) {
71+
// nodes[i].next = nodes[j];
72+
// i++;
73+
// if (i >= j) break;
74+
// nodes[j].next = nodes[i];
75+
// j--;
76+
// }
77+
78+
// nodes[i].next = null;
79+
80+
// return head
81+
// };

example/3.Linkedlist/0002.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function(l1, l2) {
14+
15+
};
16+
17+
const myLinkedList = new LinkedList();
18+
myLinkedList.prepend(2);
19+
myLinkedList.append(4);
20+
myLinkedList.append(3);
21+
22+
const myLinkedList2 = new LinkedList();
23+
myLinkedList2.prepend(5);
24+
myLinkedList2.append(6);
25+
myLinkedList2.append(4);
26+
27+
const myLinkedList3 = new LinkedList();
28+
myLinkedList3.prepend(0);
29+
30+
const myLinkedList4 = new LinkedList();
31+
myLinkedList4.prepend(0);
32+
33+
const myLinkedList5 = new LinkedList();
34+
myLinkedList5.prepend(9);
35+
myLinkedList5.append(9);
36+
myLinkedList5.append(9);
37+
myLinkedList5.append(9);
38+
myLinkedList5.append(9);
39+
myLinkedList5.append(9);
40+
myLinkedList5.append(9);
41+
42+
const myLinkedList6 = new LinkedList();
43+
myLinkedList6.prepend(9);
44+
myLinkedList6.append(9);
45+
myLinkedList6.append(9);
46+
myLinkedList6.append(9);
47+
48+
const myLinkedList7 = new LinkedList();
49+
myLinkedList7.prepend(1);
50+
myLinkedList7.append(2);
51+
myLinkedList7.append(3);
52+
53+
const myLinkedList8 = new LinkedList();
54+
myLinkedList8.prepend(4);
55+
myLinkedList8.append(5);
56+
myLinkedList8.append(6);
57+
58+
const myLinkedList9 = new LinkedList();
59+
myLinkedList3.prepend(9);
60+
61+
const myLinkedList10 = new LinkedList();
62+
myLinkedList4.prepend(9);
63+
64+
65+
const executeList = (l1, l2) => addTwoNumbers(l1.head, l2.head)
66+
67+
const execute = (l1, l2) => {
68+
console.log('travers')
69+
console.log(l1.toString());
70+
console.log(l2.toString());
71+
const list1 = executeList(l1, l2)
72+
console.log(traversList(list1))
73+
}
74+
75+
execute(myLinkedList, myLinkedList2) // [2,4,3] [5,6,4] // [7,0,8]
76+
execute(myLinkedList3, myLinkedList4) // [0] [0] // [0] ???
77+
execute(myLinkedList5, myLinkedList6) // [9,9,9,9,9,9,9] [9,9,9,9] // [8,9,9,9,0,0,0,1]
78+
execute(myLinkedList7, myLinkedList8) // [1,2,3] [4,5,6] // [5,7,9]
79+
execute(myLinkedList9, myLinkedList10) // [9] [9] // [8,1]

example/3.Linkedlist/0138.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* // Definition for a _Node.
3+
* function _Node(val, next, random) {
4+
* this.val = val;
5+
* this.next = next;
6+
* this.random = random;
7+
* };
8+
*/
9+
10+
/**
11+
* @param {_Node} head
12+
* @return {_Node}
13+
*/
14+
var copyRandomList = function(head) {
15+
map = new Map();
16+
17+
if (head === null) return null;
18+
if (map.has(head)) return map.get(head);
19+
20+
const copy = new LinkedListNode(head.val);
21+
map.set(head, copy);
22+
copy.next = copyRandomList(head.next);
23+
copy.random = map.get(head.random) || null;
24+
return copy;
25+
};
26+
27+
const myLinkedList = new LinkedList();
28+
myLinkedList.prepend(1);
29+
myLinkedList.append(2);
30+
myLinkedList.append(3);
31+
myLinkedList.append(4);
32+
33+
const myLinkedList2 = new LinkedList();
34+
myLinkedList2.prepend(1);
35+
myLinkedList2.append(2);
36+
myLinkedList2.append(3);
37+
myLinkedList2.append(4);
38+
myLinkedList2.append(5);
39+
40+
const myLinkedList3 = new LinkedList();
41+
myLinkedList3.prepend(2);
42+
myLinkedList3.append(4);
43+
myLinkedList3.append(6);
44+
myLinkedList3.append(8);
45+
46+
const myLinkedList4 = new LinkedList();
47+
myLinkedList4.prepend(2);
48+
myLinkedList4.append(4);
49+
myLinkedList4.append(6);
50+
myLinkedList4.append(8);
51+
myLinkedList4.append(10);
52+
53+
54+
const executeList = list => copyRandomList(list.head)
55+
56+
const execute = list => {
57+
console.log('travers')
58+
console.log(list.toString());
59+
const list1 = executeList(list)
60+
console.log(traversList(list1))
61+
}
62+
63+
execute(myLinkedList) // [1,2,3,4] // [1,4,2,3]
64+
execute(myLinkedList2) // [1,2,3,4,5] // [1,5,2,4,3]
65+
execute(myLinkedList3) // [2,4,6,8] // [2,8,4,6]
66+
execute(myLinkedList4) // [2,4,6,8,10] // [2,10,4,8,6]

example/3.Linkedlist/0141.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function(head) {
14+
15+
};

example/3.Linkedlist/0143.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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} head
10+
* @return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
var reorderList = function(head) {
13+
14+
};
15+
16+
const myLinkedList = new LinkedList();
17+
myLinkedList.prepend(1);
18+
myLinkedList.append(2);
19+
myLinkedList.append(3);
20+
myLinkedList.append(4);
21+
22+
const myLinkedList2 = new LinkedList();
23+
myLinkedList2.prepend(1);
24+
myLinkedList2.append(2);
25+
myLinkedList2.append(3);
26+
myLinkedList2.append(4);
27+
myLinkedList2.append(5);
28+
29+
const myLinkedList3 = new LinkedList();
30+
myLinkedList3.prepend(2);
31+
myLinkedList3.append(4);
32+
myLinkedList3.append(6);
33+
myLinkedList3.append(8);
34+
35+
const myLinkedList4 = new LinkedList();
36+
myLinkedList4.prepend(2);
37+
myLinkedList4.append(4);
38+
myLinkedList4.append(6);
39+
myLinkedList4.append(8);
40+
myLinkedList4.append(10);
41+
42+
43+
const executeList = list => reorderList(list.head)
44+
45+
const execute = list => {
46+
console.log('travers')
47+
console.log(list.toString());
48+
const list1 = executeList(list)
49+
console.log(traversList(list1))
50+
}
51+
52+
execute(myLinkedList) // [1,2,3,4] // [1,4,2,3]
53+
execute(myLinkedList2) // [1,2,3,4,5] // [1,5,2,4,3]
54+
execute(myLinkedList3) // [2,4,6,8] // [2,8,4,6]
55+
execute(myLinkedList4) // [2,4,6,8,10] // [2,10,4,8,6]

example/3.Linkedlist/0287.js

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

example/README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ Better order to solve problems
4242
904. Fruit Into Baskets
4343
0.
4444
121. Best Time to Buy and Sell Stock
45-
424. Longest Repeating Character Replacement
4645
- 424. Longest Repeating Character Replacement
4746
- 567. Permutation in String
4847
- 76. Minimum Window Substring
4948
- 239. Sliding Window Maximum
5049

51-
### Linked list
50+
### [Linked list](https://github.com/Barklim/leetcode-javascript/tree/master/example/3.Linkedlist)
5251

5352
707. Design Linked List
5453
876. Middle of the Linked List
@@ -60,6 +59,13 @@ Better order to solve problems
6059
24. Swap Nodes in Pairs
6160
21. Merge Two sorted Lists
6261
141. Linked List Cycle
62+
0.
63+
143. Reorder List
64+
- 138. Copy List with Random Pointer
65+
- 2. Add Two Numbers
66+
- 287. Find the Duplicate Number
67+
- 23. Merge k Sorted Lists
68+
- 25. Reverse Nodes in k-Group
6369

6470
### HashMap
6571

0 commit comments

Comments
 (0)