Interview question 11: Delete the linked list node at O (1) Time

Source: Internet
Author: User


Method 1: Search the nodes to be deleted in sequence and delete them in the linked list. The time complexity is O (n), which does not meet the requirements of the questions.

Code:

 

Void DeleteNode (ListNode ** pListHead, ListNode * pToBeDeleted) {if (* pListHead! = NULL & pToBeDeleted! = NULL) {if (* pListHead = pToBeDeleted) {* pListHead = pToBeDeleted-> m_pNext; delete pToBeDeleted; pToBeDeleted = NULL;} else {ListNode * pCur = * pListHead; while (pCur-> m_pNext! = PToBeDeleted) {pCur = pCur-> m_pNext;} pCur-> m_pNext = pToBeDeleted-> m_pNext; delete pToBeDeleted; pToBeDeleted = NULL ;}} else {cout <"linked list is empty! "<Endl ;}} void DeleteNode (ListNode ** pListHead, ListNode * pToBeDeleted) {if (* pListHead! = NULL & pToBeDeleted! = NULL) {if (* pListHead = pToBeDeleted) {* pListHead = pToBeDeleted-> m_pNext; delete pToBeDeleted; pToBeDeleted = NULL;} else {ListNode * pCur = * pListHead; while (pCur-> m_pNext! = PToBeDeleted) {pCur = pCur-> m_pNext;} pCur-> m_pNext = pToBeDeleted-> m_pNext; delete pToBeDeleted; pToBeDeleted = NULL ;}} else {cout <"linked list is empty! "<Endl ;}}


Method 2: copy the content of the next node to the node to be deleted and overwrite the original content. Then, delete the next node, which is equivalent to deleting the current node, you do not need to scan the linked list from the front to the back.

Note: In special cases, the deleted node is the end node.


Two cases: 1. The linked list has multiple nodes (only sequential search is supported); 2. the linked list has only one node.


Code:

 


# Include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext ;}; // Delete the specified node void DeleteNode (ListNode ** pListHead, ListNode * pToBeDeleted) {if (* pListHead! = NULL & pToBeDeleted! = NULL) {if (pToBeDeleted-> m_pNext! = NULL) // The deleted node is not the end node {ListNode * pNext = pToBeDeleted-> m_pNext; pToBeDeleted-> m_nValue = pNext-> m_nValue; pToBeDeleted-> m_pNext = pNext-> m_pNext; delete pNext; pNext = NULL;} else if (* pListHead = pToBeDeleted) // The linked list contains only one node {delete pToBeDeleted; pToBeDeleted = NULL; * pListHead = NULL;} else // The deleted node is the end node {ListNode * pCur = * pListHead; while (pCur-> m_pNext! = PToBeDeleted) {pCur = pCur-> m_pNext;} pCur-> m_pNext = NULL; delete pToBeDeleted; pToBeDeleted = NULL;} else {cout <"linked list is empty! "<Endl ;}// create a linked list, enter the value of the Start Node and end node, and enter-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; listNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead;} else {pListNode = new ListNode (); cin> pListNode-> m_nValue; if (pListNode-> m_nValue =-1) {break ;} PListNode-> m_pNext = NULL; pCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode ;}}// print the linked table void PrintList (ListNode * pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pHead = NULL; // create a linked list CreateList (pHead ); printList (pHead); // Delete the header node DeleteNode (& pHead, pHead); PrintList (pHead); // Delete the second node if (pHead! = NULL) {DeleteNode (& pHead, pHead-> m_pNext); PrintList (pHead) ;}// Delete the End Node ListNode * pCur = pHead; if (pCur! = NULL) {while (pCur-> m_pNext! = NULL) {pCur = pCur-> m_pNext;} DeleteNode (& pHead, pCur); PrintList (pHead);} system ("pause"); return 0 ;} # include "stdafx. h "# include <iostream> using namespace std; struct ListNode {int m_nValue; ListNode * m_pNext ;}; // Delete the specified node void DeleteNode (ListNode ** pListHead, ListNode * pToBeDeleted) {if (* pListHead! = NULL & pToBeDeleted! = NULL) {if (pToBeDeleted-> m_pNext! = NULL) // The deleted node is not the end node {ListNode * pNext = pToBeDeleted-> m_pNext; pToBeDeleted-> m_nValue = pNext-> m_nValue; pToBeDeleted-> m_pNext = pNext-> m_pNext; delete pNext; pNext = NULL;} else if (* pListHead = pToBeDeleted) // The linked list contains only one node {delete pToBeDeleted; pToBeDeleted = NULL; * pListHead = NULL;} else // The deleted node is the end node {ListNode * pCur = * pListHead; while (pCur-> m_pNext! = PToBeDeleted) {pCur = pCur-> m_pNext;} pCur-> m_pNext = NULL; delete pToBeDeleted; pToBeDeleted = NULL;} else {cout <"linked list is empty! "<Endl ;}// create a linked list, enter the value of the Start Node and end node, and enter-1 to end void CreateList (ListNode * & pHead) {ListNode * pListNode = NULL; listNode * pCurLastNode = NULL; bool isHead = true; while (1) {if (isHead) {pHead = new ListNode (); cin> pHead-> m_nValue; pHead-> m_pNext = NULL; isHead = false; pCurLastNode = pHead;} else {pListNode = new ListNode (); cin> pListNode-> m_nValue; if (pListNode-> m_nValue =-1) {break;} pListNode-> m_pNext = NULL; PCurLastNode-> m_pNext = pListNode; pCurLastNode = pListNode; }}// print the void PrintList (ListNode * pHead) {if (pHead! = NULL) {ListNode * pCur = pHead; while (pCur! = NULL) {cout <pCur-> m_nValue <""; pCur = pCur-> m_pNext;} cout <endl;} else {cout <"linked list is empty! "<Endl ;}} int _ tmain (int argc, _ TCHAR * argv []) {ListNode * pHead = NULL; // create a linked list CreateList (pHead ); printList (pHead); // Delete the header node DeleteNode (& pHead, pHead); PrintList (pHead); // Delete the second node if (pHead! = NULL) {DeleteNode (& pHead, pHead-> m_pNext); PrintList (pHead) ;}// Delete the End Node ListNode * pCur = pHead; if (pCur! = NULL) {while (pCur-> m_pNext! = NULL) {pCur = pCur-> m_pNext;} DeleteNode (& pHead, pCur); PrintList (pHead);} system ("pause"); return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.