File tree 2 files changed +154
-0
lines changed
2 files changed +154
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
8
+ < title > Document</ title >
9
+ </ head >
10
+
11
+ < body >
12
+ < script >
13
+ // Source : https://leetcode.com/problems/swap-nodes-in-pairs/
14
+ // Author : 悬笔e绝
15
+ // Date : 2018-03-23
16
+
17
+ /**
18
+ * @param {ListNode } head
19
+ * @return {ListNode }
20
+ */
21
+ var swapPairs = function ( head ) {
22
+ if ( ! head )
23
+ return null ;
24
+ var arr = [ ] ;
25
+ //把链表的每个节点放到arr中,且next都置null
26
+ while ( head ) {
27
+ var next = head . next ;
28
+ head . next = null ;
29
+ arr . push ( head ) ;
30
+ head = next ;
31
+ }
32
+
33
+ var len = arr . length ;
34
+ //数组每两个一组交换顺序
35
+ for ( var i = 0 ; i < len ; i += 2 ) {
36
+ var a = arr [ i ] ;
37
+ var b = arr [ i + 1 ] ;
38
+ //数组长度为奇数的情况
39
+ if ( ! b )
40
+ continue ;
41
+ //互换
42
+ arr [ i ] = b ;
43
+ arr [ i + 1 ] = a ;
44
+ }
45
+ //把两两互换之后的节点串起来~
46
+ for ( var i = 0 ; i < len - 1 ; i ++ )
47
+ arr [ i ] . next = arr [ i + 1 ] ;
48
+ //返回头节点
49
+ return arr [ 0 ] ;
50
+ } ;
51
+
52
+ // Definition for singly-linked list.
53
+ function ListNode ( val ) {
54
+ this . val = val ;
55
+ this . next = null ;
56
+ }
57
+
58
+ //测试 1->2->3->4->5
59
+ var N5 = new ListNode ( 5 ) ;
60
+ var N4 = new ListNode ( 4 ) ;
61
+ N4 . next = N5 ;
62
+ var N3 = new ListNode ( 3 ) ;
63
+ N3 . next = N4 ;
64
+ var N2 = new ListNode ( 2 ) ;
65
+ N2 . next = N3 ;
66
+ var N1 = new ListNode ( 1 ) ;
67
+ N1 . next = N2 ;
68
+
69
+ console . log ( swapPairs ( N1 ) ) ;
70
+
71
+ </ script >
72
+ </ body >
73
+
74
+ </ html >
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7
+ < meta http-equiv ="X-UA-Compatible " content ="ie=edge ">
8
+ < title > Document</ title >
9
+ </ head >
10
+
11
+ < body >
12
+ < script >
13
+ // Source : https://leetcode.com/problems/reverse-nodes-in-k-group/
14
+ // Author : 悬笔e绝
15
+ // Date : 2018-03-23
16
+
17
+ /**
18
+ * @param {ListNode } head
19
+ * @param {number } k
20
+ * @return {ListNode }
21
+ */
22
+
23
+ //其实就是24的进阶版,关键使用了数组的reserve倒置方法
24
+ var reverseKGroup = function ( head , k ) {
25
+ var ans = [ ] ;
26
+
27
+ while ( head ) {
28
+ ans . push ( new ListNode ( head . val ) ) ;
29
+ head = head . next ;
30
+ }
31
+
32
+ if ( ! ans . length )
33
+ return null ;
34
+
35
+ var len = ans . length ;
36
+ var res = [ ] ;
37
+
38
+ for ( var i = 0 ; i < len ; i += k ) {
39
+ var tmp ;
40
+ //如果超出长度,截取最后一段,且不用倒置
41
+ if ( i + k > len )
42
+ tmp = ans . slice ( i , len ) ;
43
+ else {
44
+ //没有超出长度就截取并倒置,比如0到3,截取前3个
45
+ tmp = ans . slice ( i , i + k ) ;
46
+ tmp . reverse ( ) ;
47
+ }
48
+ //把tmp不断的加到res中
49
+ Array . prototype . push . apply ( res , tmp ) ;
50
+ }
51
+ //把res中的节点串起来
52
+ for ( var i = 0 , len = res . length - 1 ; i < len ; i ++ )
53
+ res [ i ] . next = res [ i + 1 ] ;
54
+
55
+ return res [ 0 ] ;
56
+ } ;
57
+
58
+ // Definition for singly-linked list.
59
+ function ListNode ( val ) {
60
+ this . val = val ;
61
+ this . next = null ;
62
+ }
63
+
64
+ //测试 1->2->3->4->5
65
+ var N5 = new ListNode ( 5 ) ;
66
+ var N4 = new ListNode ( 4 ) ;
67
+ N4 . next = N5 ;
68
+ var N3 = new ListNode ( 3 ) ;
69
+ N3 . next = N4 ;
70
+ var N2 = new ListNode ( 2 ) ;
71
+ N2 . next = N3 ;
72
+ var N1 = new ListNode ( 1 ) ;
73
+ N1 . next = N2 ;
74
+
75
+ console . log ( reverseKGroup ( N1 , 3 ) ) ;
76
+
77
+ </ script >
78
+ </ body >
79
+
80
+ </ html >
You can’t perform that action at this time.
0 commit comments