凹凸Man

When you're lying, it's hard to tell a story backwards, because there's no real memory of what happened.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

单向链表反向排序算法~

Posted on 2010-05-19 00:32  凹凸Man  阅读(677)  评论(0)    收藏  举报

class Node
{
public:
 int data;
 Node* next;
};

Node* ReverseList(Node *head)
{
 if((head->next == NULL) || (head == NULL))
  return head;
 
 Node *temp1 = head;
 Node *temp2;
 Node *temp3 = temp1->next;
 temp1->next = NULL;
 while(temp3->next != NULL)
 {
  temp2 = temp3;
  temp3 = temp3->next;
  temp2->next = temp1;
  temp1 = temp2;
 }
 temp3->next = temp1;
 return temp3;
}