[Huawei Machine Test exercises]49. Inserting a node into an ascending one-way list

Source: Internet
Author: User

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

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.