![Learn Data Structures and Algorithms with Golang](https://wfqqreader-1252317822.image.myqcloud.com/cover/744/36698744/b_36698744.jpg)
上QQ阅读APP看书,第一时间看更新
The NodeBetweenValues method
The NodeBetweenValues method of the LinkedList class returns the node that has a property lying between the firstProperty and secondProperty values. The method traverses the list to find out whether the firstProperty and secondProperty integer properties match on consecutive nodes, as follows:
//NodeBetweenValues method of LinkedList
func (linkedList *LinkedList) NodeBetweenValues(firstProperty int,secondProperty int) *Node{
var node *Node
var nodeWith *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
if node.previousNode != nil && node.nextNode != nil {
if node.previousNode.property == firstProperty && node.nextNode.property ==
secondProperty{
nodeWith = node
break;
}
}
}
return nodeWith
}
The example output after the node between the values method was invoked with 1 and 5 is shown in the following screenshot. The nextNode of the lastNode is set to the node with a value of 5. The node with a property value of 7 is between the nodes with property values of 1 and 5:
![](https://epubservercos.yuewen.com/AA6936/19470380301498306/epubprivate/OEBPS/Images/c0578a2b-1158-44d6-b779-2b695aaf820c.png?sign=1739277149-82j1Lk7cwlsBCFFuQjG8PJOR7wPX4SzI-0-9bbe7a4f3ed600c7f374681573e55360)
Let's take a look at the AddToHead method in the next section.