leetcode 206 Reverse Linked List

来源:互联网 发布:mac 安装c4d r17 编辑:程序博客网 时间:2024/04/27 13:47

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;    }};



0 0
原创粉丝点击