翻转链表

来源:互联网 发布:印度与中国 知乎 编辑:程序博客网 时间:2024/06/09 15:22
/** * Definition of ListNode *  * class ListNode { * public: *     int val; *     ListNode *next; *  *     ListNode(int val) { *         this->val = val; *         this->next = NULL; *     } * } */class Solution{public:/** @param head: n* @return: The new head of reversed linked list.*/ListNode * reverse(ListNode * head){// write your code hereif(head==NULL){    return NULL;}ListNode * p1 = NULL;ListNode *p2 = head->next;while (p2!=NULL){            head->next=p1;            p1=head;            head=p2;            p2=p2->next;}        head->next=p1;        return head;}};

原创粉丝点击