Topic
Describe:
输入一个升序单向链表和一个链表节点,向单向链表中按升序插入这个节点。输入为空指针的情况视为异常,另外不考虑节点值相等的情况。链表结点定义如下:struct ListNode{ int m_nKey; ListNode* m_pNext;};详细描述:
Interface description
Prototype:
ListNode* InsertNodeToList(ListNode* pListHead, ListNode* pInsertNode);
Input parameters:
ListNode* pListHead 单向链表 ListNode* pInsertNode 新插入节点
Output parameters (the memory area pointed to by the pointer is guaranteed to be valid):
ListNode* pListHead 单向链表
return value:
正常插入节点返回链表头指针,其它异常返回空指针
Practice Stage:
Code
/* ---------------------------------------* Date: 2015-07-04* sjf0115* topic: Inserting a node into an ascending one-way list * Source: Huawei Machine Test Exercises------------- ----------------------------*/#include <iostream>#include "OJ.h"using namespace Std;/ * Function: Enter an ascending one-way linked list and a linked list node, which is treated as an exception when inserting this node into a one-way linked list in ascending order, and regardless of the node value equality. Input: listnode* plisthead One-way linked list listnode* pinsertnode new insert node output: listnode* plisthead One-way linked list return: Normal Insert node returns the chain header pointer, other different Often returns a null pointer * /ListNode*Insertnodetolist (ListNode*Plisthead, ListNode*Pinsertnode) {if(Plisthead== NULL ||Pinsertnode== NULL){return NULL; }//if //Add head nodeListNode*Dummy= NewListNode (); Dummy -M_pnext=Plisthead; ListNode*P=Plisthead; ListNode*Pre=Dummy while(p) {//Insert if(p -M_nkey>Pinsertnode -M_nkey) {break; }//ifPre=P P=P -M_pnext; }//while //InsertPinsertnode -M_pnext=P Pre -M_pnext=Pinsertnode;returnDummy -M_pnext;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Huawei Machine Test exercises]49. Inserting a node into an ascending one-way list