﻿ VB.NET exercises and solutions: min max items of Linked List

# VB.NET exercises and solutions: min max items of Linked List

## Singly linked list

### Min and Max items of singly linked list

Step 5: Showing the minimum and maximum items

To find the minimum item of the Singly Linked List, you need to compare each item of the list by allowing a min variable to point to the first item of the list then starting to compare its data with its next item. If the data of its next item is less than the data of the min, simply allow the min to catch the next item.

'Find the min item
Public Overrides Function findmin() As ListNode
Dim t, min As ListNode
min = pfirst
t = pfirst
While Not IsNothing(t)
If (min.val > t.val) Then
min = t
End If
t = t.nextlk
End While
Return min

End Function

Find the maximum item of the LinkedList can be done similarly as finding the minimum item. You need to compare each item of the list by allowing a max variable to point to the first item of the list then starting to compare its data with its next item. If the data of its next item is greater than the data of the max, simply allow the max to catch the next item.

'Find the max item
Public Overrides Function findmax() As ListNode
Dim t, max As ListNode
max = pfirst
t = pfirst
While Not IsNothing(t)
If (max.val < t.val) Then
max = t
End If
t = t.nextlk
End While
Return max

End Function