Reverse Linked List 链表逆转 简单题

来源:互联网 发布:周润发 让子弹飞 知乎 编辑:程序博客网 时间:2024/05/16 18:23

Reverse Linked List

Reverse a singly linked list.

click to show more hints.

/** * 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) {             ListNode *cur,*pre,*next;        cur=head;        pre=NULL;        while(cur!=NULL)        {            next=cur->next;            if(next==NULL)                head=cur;            cur->next=pre;            pre=cur;            cur=next;        }        return head;    }};
0 0
原创粉丝点击