刷题:反转链表

  • 题目:输入一个链表,反转链表后,输出新链表的表头。
  • idea:先得到下一个node,然后当前node指向前一个。
  • code
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode *next, *prev=NULL, *cur=pHead;
        while(cur){
            next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
};

猜你喜欢

转载自blog.csdn.net/nickkissbaby_/article/details/89255503