Harris Calvin
Harris Calvin

Reputation: 473

How to iterate through Linked List

I have looked around and I can't really find an answer I can understand or it doesn't apply to me. I have this class:

class Node
{
    public int value;
    public Node next;
}

And I have a member variable called head which is the head of the single linked list. Now I am trying to iterate through the different nodes of the linked list to search for a specific value. I know if I do it manually then I would do head.next.next.next.next.value if I want the value of the 5th node. This would quickly get tedious for a linked list of very large size so my question is how can I create some loop to iterate through this so that I can check the value variable in each node of the linked list?

Upvotes: 14

Views: 51300

Answers (4)

Andrew900460
Andrew900460

Reputation: 721

I know this is an old post, but this is what is popping up on google, and I do have a good alternative to the current best answer (not including the desired value condition)

LinkedList<T> list = new LinkedList<T>();
for(LinkedListNode<T> node = list.First; node != null; node=node.Next){
    //do stuff
}

This version obviously uses a for loop and moves the variable declaration and increment to one line allowing you to condense and beautify your code.


Later Edit: If you wish to make your own Linked List (for whatever reason, cough cough), try this:

// assumed implementation of class
LinkedList myLinkedList = new LinkedList();

int searchForValue = 5; // origial poster wants to search for value
Node resultNode = null;
int resultIndex = 0;
for(Node iNode = myLinkedList.head; iNode!=null; iNode = myLinkedList.next) {
    if(iNode.value == searchForValue) {
        resultNode = iNode;
        break;
    }
    resultIndex++;
}

if(resultNode!=null)
    Console.WriteLine($"Found value at index {resultIndex} of {resultNode.value}");

I decided to add this alternative answer after so many years because I realized that I didn't actually answer the original poster's question 100% the way they might've wanted it to be answered (maybe they wanted to make their own linked list). So I am doing it justice now by providing a more "in-tune" answer.

But either will suffice!

Upvotes: 33

Botz3000
Botz3000

Reputation: 39610

For this kind of list, you usually keep a reference to the current node (which starts with the head), and after each iteration, you change the value of that reference to the next node. When currentNode becomes null, you have reached the end of the list, as the last element doesn't have a next element.

Something like this:

Node currentNode = head;
while (currentNode != null) {
    // do stuff with currentNode.value
    currentNode = currentNode.Next;
}

By the way, the BCL already contains some useful classes for this sort of task:

  • List<T>, which internally uses arrays to store elements and provides random access to them
  • LinkedList<T>, which uses the same principle as your custom class.

But maybe you need to do it the way you do for some reason :)

Upvotes: 5

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17370

Try this basic iteration:

Node tmp = head;
while (tmp != null)
{
    //do your checking...
    tmp = tmp.next;
}

Upvotes: 2

William
William

Reputation: 1875

You iterate through your class as follows:

var currentNode = head;
while ((currentNode != null) && (currentNode.Value != desiredValue))
   currentNode = currentNode.next;

When the while loop completes, currentNode will be null or contain a node with the desired value.

Upvotes: 13

Related Questions