Skip to content

Commit 4bdcd73

Browse files
BarklimBarklim
Barklim
authored and
Barklim
committed
Create 0024.js
1 parent caa021e commit 4bdcd73

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

0024-swap-nodes-in-pairs.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ var swapPairs = function(head) {
2929

3030
return result;
3131
};
32+
33+
// var swapPairs = function(head) {
34+
// let dummy = new LinkedListNode()
35+
// dummy.next = head
36+
37+
// current = dummy
38+
39+
// while (current.next && current.next.next) {
40+
// first = current.next
41+
// second = current.next.next
42+
43+
// first.next = second.next
44+
// second.next = first
45+
// current.next = second
46+
47+
// current = first
48+
// }
49+
50+
// return dummy.next
51+
// };

example/3.Linkedlist/0024.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 {ListNode}
11+
*/
12+
var swapPairs = 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+
24+
const myLinkedList3 = new LinkedList();
25+
myLinkedList3.prepend(1);
26+
27+
const myLinkedList4 = new LinkedList();
28+
myLinkedList4.prepend(1);
29+
myLinkedList4.append(2);
30+
myLinkedList4.append(3);
31+
32+
const myLinkedList5 = new LinkedList();
33+
myLinkedList5.prepend(1);
34+
myLinkedList5.append(2);
35+
myLinkedList5.append(3);
36+
myLinkedList5.append(4);
37+
myLinkedList5.append(5);
38+
myLinkedList5.append(6);
39+
40+
const executeList = (list, n) => swapPairs(list.head)
41+
42+
const execute = list => {
43+
console.log('travers')
44+
console.log(list.toString());
45+
const list1 = executeList(list)
46+
console.log(traversList(list1))
47+
}
48+
49+
execute(myLinkedList) // 1,2,3,4, // 1,2,3,5
50+
execute(myLinkedList2) // [] // []
51+
execute(myLinkedList3) // [1] // [1]
52+
execute(myLinkedList4) // 1,2,3 // 2,1,3
53+
execute(myLinkedList5) // 1,2,3,4,5,6 // 2,1,4,3,6,5
54+
55+

example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Better order to solve problems
5757
234. Palindrome Linked List
5858
83. Remove Duplicates from Sorted List
5959
19. Remove Nth Node from End of List
60-
43. Swap Nodes in Pairs
60+
24. Swap Nodes in Pairs
6161
21. Merge Two sorted Lists
6262
141. Linked List Cycle
6363

0 commit comments

Comments
 (0)