Skip to content

Commit f6faf98

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
Create 0234.js
1 parent bec0c9c commit f6faf98

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

0234-palindrome-linked-list.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,46 @@ var isPalindrome = function(head) {
2828

2929
return a === b;
3030
};
31+
32+
// var isPalindrome = function(head) {
33+
// var middle = function(head) {
34+
// let slow = head;
35+
// let fast = head;
36+
37+
// while (fast && fast.next) {
38+
// slow = slow.next;
39+
// fast = fast.next.next;
40+
// }
41+
42+
// return slow;
43+
// }
44+
45+
// var reverse = function(head) {
46+
// let prev = null;
47+
// let tail = head;
48+
49+
// while (tail) {
50+
// const next = tail.next;
51+
// tail.next = prev;
52+
// prev = tail;
53+
// tail = next;
54+
// }
55+
56+
// return prev;
57+
// }
58+
59+
// mid = middle(head)
60+
// second = reverse(mid)
61+
// first = head
62+
63+
// while (first && second) {
64+
// // !!! value, val
65+
// if (first.value !== second.value) {
66+
// return false
67+
// }
68+
// first = first.next
69+
// second = second.next
70+
// }
71+
72+
// return true
73+
// };

example/3.Linkedlist/0234.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 {boolean}
11+
*/
12+
var isPalindrome = function(head) {
13+
14+
};
15+
16+
const myLinkedList = new LinkedList();
17+
myLinkedList.prepend(1);
18+
myLinkedList.append(2);
19+
myLinkedList.append(2);
20+
myLinkedList.append(1);
21+
22+
const myLinkedList2 = new LinkedList();
23+
myLinkedList2.prepend(1);
24+
myLinkedList2.append(2);
25+
26+
const myLinkedList3 = new LinkedList();
27+
myLinkedList3.prepend(1);
28+
myLinkedList3.append(2);
29+
myLinkedList3.append(4);
30+
myLinkedList3.append(2);
31+
myLinkedList3.append(1);
32+
33+
const myLinkedList4 = new LinkedList();
34+
myLinkedList4.prepend(1);
35+
myLinkedList4.append(2);
36+
myLinkedList4.append(4);
37+
myLinkedList4.append(4);
38+
myLinkedList4.append(2);
39+
myLinkedList4.append(1);
40+
41+
const myLinkedList5 = new LinkedList();
42+
myLinkedList5.prepend(1);
43+
myLinkedList5.append(2);
44+
myLinkedList5.append(4);
45+
myLinkedList5.append(5);
46+
myLinkedList5.append(2);
47+
myLinkedList5.append(1);
48+
49+
const executeList = list => isPalindrome(list.head)
50+
51+
const execute = list => {
52+
console.log(list.toString());
53+
console.log(executeList(list));
54+
}
55+
56+
console.log('middleNode');
57+
58+
execute(myLinkedList) // 1 2 2 1 // true
59+
execute(myLinkedList2) // 1 2 // false
60+
execute(myLinkedList3) // 1 2 4 2 1 // true
61+
execute(myLinkedList4) // 1 2 4 4 2 1 // true
62+
execute(myLinkedList5) // 1 2 4 5 2 1 // false

0 commit comments

Comments
 (0)