In the past we have already covered reversing a Linked List via Recursion.  Today we will discuss how we can reverse a Linked List using Iterative method.

Algorithm

  1. Take three pointer currentNode,nextNode and previousNode
  2. initialize currentNode to head of the linked List
  3. iterate over the linked list until currentNode is equal to null
    1. nextNode to previousNode
    2. previousNode to currentNode
    3. In the loop change currentNode to next Node
  4. return previousNode

Code

        public Node<T> ReverseIterative()
        {

            Node<T> currentNode = this.head;
            Node<T> previousNode, nextNode;
            previousNode = nextNode = null;

            while (currentNode != null)
            {
                nextNode = currentNode.Next;
                currentNode.Next = previousNode;
                previousNode = currentNode;
                currentNode = nextNode;

            }
            return previousNode;
        }