Programming Algorithm-code of the first public node of two linked lists (c)

Source: Internet
Author: User
Code of the first public node of the two linked lists (c)


Address: http://blog.csdn.net/caroline_wendy


Question: enter two linked lists to find their first public node.


Calculate the length of the linked list, move the pointer of a longer-chain table to the same distance to the same node, move the pointer of the two linked lists at the same time, and find the same element.

Time Complexity: O (N)


Code:

/* * main.cpp * *  Created on: 2014.6.12 *      Author: Spike *//*eclipse cdt, gcc 4.8.1*/#include <stdio.h>#include <stdlib.h>#include <string.h>struct ListNode {int m_nKey;ListNode* m_pNext;};size_t GetListLength (ListNode* pHead) {size_t nLength = 0;ListNode* pNode = pHead;while (pNode != NULL) {++nLength;pNode = pNode->m_pNext;}return nLength;}ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {size_t nLength1 = GetListLength(pHead1);size_t nLength2 = GetListLength(pHead2);int nLengthDif = nLength1 - nLength2;ListNode* pListHeadLong = pHead1;ListNode* pListHeadShort = pHead2;if (nLength2 > nLength1) {pListHeadLong = pHead2;pListHeadShort = pHead1;nLengthDif = nLength2 - nLength1;}for (int i=0; i<nLengthDif; ++i)pListHeadLong = pListHeadLong->m_pNext;while ((pListHeadLong != NULL) && (pListHeadShort != NULL)&& (pListHeadLong != pListHeadShort)) {pListHeadLong = pListHeadLong->m_pNext;pListHeadShort = pListHeadShort->m_pNext;}ListNode* pFirstCommonNode = pListHeadLong;return pFirstCommonNode;}int main(void){ListNode* pHead1 = new ListNode();ListNode* pHead1Node1 = new ListNode();ListNode* pHead1Node2 = new ListNode();ListNode* pHead1Node3 = new ListNode();ListNode* pHead1Node4 = new ListNode();pHead1->m_nKey = 1;pHead1Node1->m_nKey = 2;pHead1Node2->m_nKey = 3;pHead1Node3->m_nKey = 6;pHead1Node4->m_nKey = 7;pHead1->m_pNext = pHead1Node1;pHead1Node1->m_pNext = pHead1Node2;pHead1Node2->m_pNext = pHead1Node3;pHead1Node3->m_pNext = pHead1Node4;pHead1Node4->m_pNext = NULL;ListNode* pHead2 = new ListNode();ListNode* pHead2Node1 = new ListNode();pHead2->m_nKey = 4;pHead2Node1->m_nKey = 5;pHead2->m_pNext = pHead2Node1;pHead2Node1->m_pNext = pHead1Node3;ListNode* result = FindFirstCommonNode(pHead1, pHead2);printf("result = %d\n", result->m_nKey);    return 0;}

Output:

result = 6







Related Article

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.