参考书上解法:
问题:链表 *** 作不熟悉,需要强加练习!
class Solution { public: ListNode* deleteNode(ListNode* head, int val) { ListNode* pnode = nullptr; ListNode* pre = new ListNode; if(!head) return pnode; // 如果是空链表直接返回空链表 //链表只有一个节点,删除头节点也是尾节点 if(head->val==val&&head->next==nullptr){ ListNode* temp = new ListNode; temp = head; head = nullptr; // delete temp; return head; } pnode=head; while(pnode->next!=nullptr){ //要删除的节点不是尾节点 pre = pnode; ListNode* previous = new ListNode; if(pnode->val==val&&pnode->next!=nullptr){ ListNode* current = new ListNode; ListNode* pnext = new ListNode; current = pnode; pnext = current->next; current->val = pnext->val; current->next = pnext->next; break; } pnode=pnode->next; } //要删除的节点是尾节点 ListNode* cur = new ListNode; if(pnode->val==val){ // ListNode* cur = pnode; // tail = previous; cur = pre; pre->next = nullptr; } return head; } };
参考资料:
C++ single list
C single list
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)