reverse let currNode = thishead; let prevNode = null; let nextNode = null; while currNode Store next node nextNode = currNodenext; prevNode = currNodeprevious; Chan
reverse() { let currNode = this.head; //current node starts at the head of the list let prevNode = null; //initialize previous node as null let nextNode = null; //initialize next node as null
while (currNode) { //while there is a current node
nextNode = currNode.next; //store the next node
prevNode = currNode.previous; //store the previous node
currNode.next = prevNode; //change the next node of the current node to link to the previous node
currNode.previous = nextNode; //change the previous node of the current node to link to the next node
prevNode = currNode; //move the previous node one step forward
currNode = nextNode; //move the current node one step forward
}
this.tail = this.head; //reset the tail to the head
this.head = prevNode; //reset the head to the last node (which is now the first node after reversing)
return this;
原文地址: http://www.cveoy.top/t/topic/iAog 著作权归作者所有。请勿转载和采集!