@@ -8,6 +8,11 @@ const b = new ListNode(2)
88const c = new ListNode ( 4 )
99a . next = b
1010b . next = c
11+
12+ c . next = a ;
13+
14+ console . log ( isCircularLinkList ( a ) )
15+
1116const d = new ListNode ( 1 )
1217const e = new ListNode ( 3 )
1318const f = new ListNode ( 4 )
@@ -24,70 +29,8 @@ function bianli(root, vals = []) {
2429// bianli(a)
2530// bianli(d)
2631
27- /** 迭代法
28- * @param {ListNode } l1
29- * @param {ListNode } l2
30- * @return {ListNode }
31- */
32- var mergeTwoLists = function ( l1 , l2 ) {
33- let res = new ListNode ( ) ;
34- let root = res
35- while ( l1 !== null && l2 !== null ) {
36- if ( l1 . val <= l2 . val ) {
37- root . next = l1 ;
38- l1 = l1 . next ;
39- } else {
40- root . next = l2
41- l2 = l2 . next ;
42- }
43- root = root . next
44- }
45- if ( l1 != null ) {
46- root . next = l1 ;
47- }
4832
49- if ( l2 != null ) {
50- root . next = l2 ;
51- }
52- return res . next
53- } ;
54-
55- /**转化数组法
56- * @param {ListNode } l1
57- * @param {ListNode } l2
58- * @return {ListNode }
59- */
60- var mergeTwoLists2 = function ( l1 , l2 ) {
61- if ( l1 == null && l2 == null ) return l1
62- const l1res = bianli ( l1 )
63- const l2res = bianli ( l2 )
64- const resData = [ ...l1res , ...l2res ] . sort ( ( a , b ) => a - b )
65- let root = new ListNode ( resData [ 0 ] ) ;
66- let res = root ;
67- for ( let i = 1 ; i < resData . length ; i ++ ) {
68- res . next = new ListNode ( resData [ i ] )
69- res = res . next ;
70- }
71- return root
72- } ;
33+ // console.log(bianli(mergeTwoLists3(a, d)))
7334
74-
75- /**递归
76- * @param {ListNode } l1
77- * @param {ListNode } l2
78- * @return {ListNode }
79- */
80- var mergeTwoLists3 = function ( l1 , l2 ) {
81- if ( l1 == null ) {
82- return l2 ;
83- } else if ( l2 == null ) {
84- return l1 ;
85- } else if ( l1 . val < l2 . val ) {
86- l1 . next = mergeTwoLists ( l1 . next , l2 ) ;
87- return l1 ;
88- } else {
89- l2 . next = mergeTwoLists ( l1 , l2 . next ) ;
90- return l2 ;
91- }
92- } ;
93- console . log ( bianli ( mergeTwoLists3 ( a , d ) ) )
35+ import './链表/myLinkList.js'
36+ import { isCircularLinkList } from './utils/linkList.js' ;
0 commit comments