206. Reverse Linked List

来源:互联网 发布:mac怎么创建html文件 编辑:程序博客网 时间:2024/06/06 03:00

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* reverseList(ListNode* head) {        if(head == NULL)        return head;        ListNode* pre=NULL;        while( head )        {            ListNode* temp=head->next;            head->next = pre;            pre= head;            head = temp;        }        return pre;    }};


0 0
原创粉丝点击