剑指offer——链表反转

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode *list = NULL;
        ListNode *pre = NULL;
        ListNode *cur = pHead;
        while(cur != NULL){
            ListNode *next = cur->next;
            if(next == NULL){
                list = cur;
            }
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return list;
    }
};
发布了51 篇原创文章 · 获赞 29 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qwer7512090/article/details/104931843