乐趣区

关于算法:数据结构链表

把数组转成链表

         var head = [1, 2, 3, 4, 5]
         function arrayToListNode(arr) {if (arr.length === 0) return null;
            let head = {val: arr[0], next: null }
            let pre = head;
            for (let i = 1; i < arr.length; i++) {pre.next = { val: arr[i], next: null }
                pre = pre.next
                // let node = {val: arr[i], next: null }
                // pre.next = node;
                // pre = node
            }
            return head
        }
        head = arrayToListNode(head)
        console.log('head', head)
退出移动版