Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

10
Mar

Reversing a circular single linked list using pointers

Related Blog Items

Reversing a single linked list is explained in this post

this post deals with reversing circular single linked list, which is not much different from what we did there except the last reversed node points the head node, then we change the head node to point to reversed linked list….

You might be thinking why do we need to reverse a circular linked list when the list is circular, but as the list is single linked list here we are going to reverse the direction of the list.

here is the routine which reverses circular list….


typedef struct node Node;

void reverse(Node** headRef)
{
  Node* result = NULL;
  Node* current = *headRef;
  Node* next;

  while (current != NULL)
  {
    next = current->next; // tricky: note the next node
    current->next = result; // move the node onto the result
    result = current;
    current = next;
    if (current == *headRef)
    {
      //met the beginning node...
      break;
    }
  }
  (*headRef)->next = result;
  *headRef = result;
}

please let me know your comments…..

Popularity: 58%

You need to log on to convert this article into PDF


Related Blog Items

1 Comment

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image