美文网首页
24.反转链表

24.反转链表

作者: HamletSunS | 来源:发表于2019-07-31 15:32 被阅读0次

思路:
设置多个指针,指向相关节点即可

代码:

/*
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 *prior=NULL,*cur=pHead,*next;
        while(cur){
            next=cur->next;
            
            cur->next=prior;
            prior=cur;
            cur=next;
            
        }
        return prior;
    }
};

相关文章

网友评论

      本文标题:24.反转链表

      本文链接:https://www.haomeiwen.com/subject/nmltdctx.html