From 9db53b54c0b0de305e84e3e4fe17e2d2cf9e1ce5 Mon Sep 17 00:00:00 2001 From: AttackXiaoJinJin Date: Thu, 11 Oct 2018 13:43:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84javascript=E7=9A=8407?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- javascript/07_linkedlist/LinkedListAlgo.js | 31 +++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/javascript/07_linkedlist/LinkedListAlgo.js b/javascript/07_linkedlist/LinkedListAlgo.js index 39cf6e49..77695c99 100644 --- a/javascript/07_linkedlist/LinkedListAlgo.js +++ b/javascript/07_linkedlist/LinkedListAlgo.js @@ -33,7 +33,7 @@ class LinkedList { currentNode = currentNode.next pos++ } - return currentNode === null ? -1 : pos + return currentNode === null ? -1 : currentNode } // 指定元素向后插入 insert(newElement, element) { @@ -69,6 +69,9 @@ class LinkedList { } // 遍历显示所有节点 display() { + //先检查是否为环 + if(this.checkCircle()) return false + let currentNode = this.head while (currentNode !== null) { console.log(currentNode.element) @@ -89,6 +92,30 @@ class LinkedList { this.head = root } + //增强尾插法可读性,便于初学者理解 + reverseList1(){ + //head节点即哨兵,作用就是使所有链表, + // 包括空链表的头节点不为null,并使对单链表的插入、删除操作不需要区分是否为空表或是否在第一个位置进行, + // 从而与其他位置的插入、删除操作一致 + //所以反转链表的时候不需要带上head节点 + let currentNode=this.head.next + //第一个节点头结点让其指向null + let previousNode=null + while(currentNode!==null){ + //务必先保留下一节点的指针地址 + let nextNode=currentNode.next + //第一次是null + currentNode.next=previousNode + //此时将previousNode赋值为当前节点, + // 那么下次循环的时候,方便下次的currentNode指向previousNode + previousNode=currentNode + //抬走,下一个! + currentNode=nextNode + } + //最后将反转好的链表加上头节点 + this.head.next=previousNode + } + // 自己一开始瞎想的。差距啊 reverseList2() { let currentNode = this.head.next @@ -122,6 +149,8 @@ class LinkedList { } // 删除倒数第k个节点 removeByIndexFromEnd(index) { + //务必先判断是否是 环链表 + if(this.checkCircle()) return false let pos = 1 this.reverseList() let currentNode = this.head.next