您的位置:首页 > 其它

leetcode 206 Reverse Linked List

2016-06-23 22:39 411 查看
Reverse a singly linked list.

click to show more hints.

Subscribe to see which companies asked this question

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverse(ListNode *head) {
if(head == NULL) return head;
ListNode *temp = reverse(head->next);

head->next=NULL;

if(temp!=NULL)
temp->next = head;
temp = head;
return temp;
}

ListNode* reverseList(ListNode* head) {
ListNode *temp = head;
if(temp==NULL) return NULL;

while(temp->next!=NULL) {
temp = temp->next;
}

ListNode *res = reverse(head);

return temp;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: