0

Possible Duplicate:
Reverse a singly linked list

I have looked up the link Reverse a singly linked list, but I really did not get the answer. So I reopen this question.

I'm trying to reverse a link list, the function is here:

void reverse (SLINK list)
{
  NODE *p_tmp_node;
  SLINK p_tmp_list;
  p_tmp_list = list->next;
  list->next = NULL;

  while (NULL != p_tmp_list)
  {
    p_tmp_node = p_tmp_list;
    p_tmp_list = p_tmp_list->next; // A
    p_tmp_node->next = list->next;
    list->next = p_tmp_node;
    //p_tmp_list = p_tmp_list->next; // B
  }
}

My question is: The statement A is the same with statement B, but if B performs instead of A, it could not get a expected result. what's different between A and B?

The node struct just like this:

typedef struct tag_node
{
  int elem;
  struct tag_node *next;
} NODE, *SLINK; 
Community
  • 1
  • 1
Vincent Zhang
  • 97
  • 1
  • 6

0 Answers0