标签:
Reverse a singly linked list.
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* head) { 12 if (head == NULL) return NULL; 13 14 ListNode* p = head->next; 15 head->next = NULL; 16 17 while (p != NULL) { 18 ListNode* q = p->next; 19 p->next = head; 20 head = p; 21 p = q; 22 } 23 24 return head; 25 } 26 };
[LeetCode] Reverse Linked List
标签:
原文地址:http://www.cnblogs.com/vincently/p/4780233.html