How do you write a function that can reverse a linked-list? (Cisco System)


void reverselist(void)
{
if(head==0)
return;
if(head->next==0)
return;
if(head->next==tail)
{
head->next = 0;
tail->next = head;
}
else
{
node* pre = head;
node* cur = head->next;
node* curnext = cur->next;
head->next = 0;
cur->next = head;
for(; curnext!=0; )
{
cur->next = pre;
pre = cur;
cur = curnext;
curnext = curnext->next;
}
curnext->next = cur;
}
}



Explore posts in the same categories: C/C++ Interview Questions


BOOKMARK THIS : del.icio.us | Digg it | Furl | reddit |


Related Questions :

  • What is Linked List ?
  • Linked List is one of the fundamental data structures. It consists of a sequence of nodes, each containing arbitrary...
  • How can I search for data in a linked list?
  • Unfortunately, the only way to search a linked list is with a linear search, because the only way a...
  • What member function places a new node at the end of the linked list?
  • The appendNode() member function places a new node at the end of the linked list. The appendNode() requires an integer...
  • How do you write a function that can reverse a linked-list?
  • void reverselist(void) { if(head==0) return; if(head->next==0) return; if(head->next==tail) { head->next = 0; tail->next = head; } else { node* pre = head; node* cur = head->next; node* curnext = cur->next; head->next = 0; cur->next = head; for(; curnext!=0;...
  • Whether Linked List is linear or Non-linear data structure?
  • According to Access strategies Linked list is a linear one.According to Storage Linked List is a Non-linear one....
  • Whether Linked List is linear or Non-linear data structure?
  • According to Access strategies Linked list is a linear one. According to Storage Linked List is a Non-linear one...
  • How can I stop my code being reverse-engineered from IL?
  • There is currently no simple way to stop code being reverse-engineered from IL. In future it is likely that IL...
  • What does each entry in the Link List called?
  • Each entry in a linked list is called a node. Think of a node as an entry that has three...
  • How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
  • You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes...
  • How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
  • You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one goes...

    Comment:

    You must be logged in to post a comment.