牛客-反轉鏈表

牛客-反轉鏈表


/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        if (pHead == NULL) return NULL;
        ListNode *cur, *p=NULL, *temp;
        cur = pHead;
        while(pHead->next!=NULL) {
            temp = cur->next;
            cur->next = p;
            p=cur;
            cur=temp;
            pHead=temp;
        }
        cur->next=p;
        return cur;

    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章