How do you write a function that can reverse a linked-list in C++?
Submitted by: Administratorvoid 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;
}
}
Submitted by: Administrator
{
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;
}
}
Submitted by: Administrator
node* reverselist(node* head)
{
if(0==head || 0==head->next)
//if head->next ==0 should return
head instead of 0;
return 0;
{
node* prev = head;
node* curr = head->next;
node* next = curr->next;
for(; next!=0; )
{
curr->next = prev;
prev = curr;
curr = next;
next = next->next;
}
curr->next = prev;
head->next = 0;
head = curr;
}
return head;
}
Submitted by: Administrator
{
if(0==head || 0==head->next)
//if head->next ==0 should return
head instead of 0;
return 0;
{
node* prev = head;
node* curr = head->next;
node* next = curr->next;
for(; next!=0; )
{
curr->next = prev;
prev = curr;
curr = next;
next = next->next;
}
curr->next = prev;
head->next = 0;
head = curr;
}
return head;
}
Submitted by: Administrator
Read Online C++ Programming Job Interview Questions And Answers
Top C++ Programming Questions
☺ | What does extern mean in a function declaration in C++? |
☺ | Write a short code using C++ to print out all odd number from 1 to 100 using a for loop |
☺ | What is C++? |
☺ | How many ways are there to initialize an int with a constant? |
☺ | Explain the ISA and HASA class relationships. How would you implement each in a class design? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |