[leetcode 206] Reverse Linked List

Reverse a singly linked list.

click to show more hints.

 

Subscribe to see which companies asked this question

 1 ListNode* reverseList(ListNode* head) 
 2     {
 3         if (!head || !head->next)
 4             return head;
 5         
 6         ListNode* p = head, *q = p->next, *r = q->next;
 7         while (r != NULL)
 8         {
 9             q->next = p;
10             p = q;
11             q = r;
12             r = r->next;
13         }
14         q->next = p;
15         head->next = NULL;
16         head = q;
17         
18         return head;
19     }

 

你可能感兴趣的:([leetcode 206] Reverse Linked List)