Reverse a singly linked list.
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* reverseList(struct ListNode* head) { 9 struct ListNode* p; 10 struct ListNode* q; 11 p = head; 12 q = head; 13 if(NULL == head) 14 return head; 15 while(q->next != NULL) 16 q = q->next; 17 while(p != q) 18 { 19 head = head->next; 20 p->next = q->next; 21 q->next = p; 22 p =head; 23 } 24 return head; 25 }