- Learn Data Structures and Algorithms with Golang
- Bhagvan Kommadi
- 77字
- 2021-06-24 15:37:46
The LastNode method
The LastNode method of LinkedList returns the node at the end of the list. The list is traversed to check whether nextNode is nil from nextNode of headNode, as follows:
//LastNode method returns the last Node
func (linkedList *LinkedList) LastNode() *Node{
var node *Node
var lastNode *Node
for node = linkedList.headNode; node != nil; node = node.nextNode {
if node.nextNode ==nil {
lastNode = node
}
}
return lastNode
}