Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How do you write a function that can reverse a linked-list?
How do you write a function that can reverse a linked-list?
✍: Guest
Answer1: 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; } } Answer2: 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; }
2012-01-17, 2652👍, 0💬
Popular Posts:
When should the method invokeLater() be used? This method is used to ensure that Swing components ar...
How To Return Top 5 Rows? - MySQL FAQs - SQL SELECT Statements with JOIN and Subqueries If you want ...
In C#, what is a weak reference? Generally, when you talk about a reference to an object in .NET (an...
How To Analyze Tables with "mysqlcheck"? - MySQL FAQs - Administrator Tools for Managing MySQL Serve...
How can I include comments in HTML? An HTML comment begins with "<!--", ends with "-->...