[Leetcode]删除链表的节点

遍历链表出错

节点结构:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
while(pHead->next!=nullptr)
        {
            ListNode* nextNode = pHead->next;
            if(nextNode->val == val)
            {
                break;
            }
            //当前节点指向下一个节点
            //前面一直写的pHead++,导致指针出错
            pHead = nextNode;  
        }

猜你喜欢

转载自www.cnblogs.com/hopping/p/12297421.html