C Language Enhancement (7) linked list intersection problem _ 2 find the intersection node of the linked list without loops, _ 2

Source: Internet
Author: User

C Language Enhancement (7) linked list intersection problem _ 2 find the intersection node of the linked list without loops, _ 2

In the previous section, we talked about how to find the intersection node to determine whether two [non-ring] linked lists are intersecting?


Question

Two head pointers of one-way linked list, such as h1 and h2, are given to determine whether the two linked lists are intersecting.


Solution steps

Ideas
  • Record the length of the linked list L1 and L2 during traversal (assuming L1> L2)
  • Traverse to find the L1-L2 node in the first linked list,
  • Linked list from the first L1-L2 node traversal, linked list two from the first node traversal, equivalent to the two linked list from the same distance with the intersection of the two locations at the same time
  • Every step forward
  • Until the first same node is found, the two linked lists can be considered to have intersection nodes,
  • This is the first intersection node.
Schematic diagram
Source code
# Include <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** 2. find the intersection nodes of the two [no-ring] linked lists. Record the length of the linked list L1 and L2 (assuming L1> L2) during traversal and find the L1-L2 node in the first linked list, then the linked list from the first L1-L2 node to traverse, linked list two from the first node traversal, each step forward until the first to find the same node, you can think that the two linked lists exist intersection node, and this point is the first intersection node * // ** linked list structure */struct ListNode {int data; ListNode * nextNode; ListNode (ListNode * node, int value) {nextNode = node; data = value ;}}; ListNode * L1; ListNode * L2;/** get chain table length */int getListL Ength (ListNode * head) {int I = 0; if (head = NULL) return 0; while (head-> nextNode! = NULL) {head = head-> nextNode; I ++;} return I;}/** get the linked list node at the specified position */ListNode * getThatListNode (ListNode * head, int pos) {int I = 0; if (head = NULL) return NULL; while (head-> nextNode! = NULL) {head = head-> nextNode; I ++; if (pos = I) return head;} return NULL ;} /** obtain the intersection node L1 of two non-circular linked lists: L2 of a longer-chain table */ListNode * getNoCircleListCrossNode (ListNode * L1, ListNode * L2) {ListNode * L_Long; listNode * L_Short; int start; int length1 = getListLength (L1); int lengh2 = getListLength (L2); if (length1> = lengh2) {L_Long = L1; L_Short = L2; start = length1-length2;} else {L_Long = L2; L_Short = L1; start = length2-length1;} L_Long = getThat ListNode (L_Long, start); while (L_Long-> nextNode! = NULL & L_Short-> nextNode! = NULL) {if (L_Long = L_Short) return L_Long; L_Long = L_Long-> nextNode; L_Short = L_Short-> nextNode;} return NULL ;} // test void testCross () {// intersection segment ListNode * node = new ListNode (NULL, 0); node = new ListNode (node, 1 ); node = new ListNode (node, 2); node = new ListNode (node, 3); // The intersection starts at L1 = new ListNode (node, 11 ); l2 = new ListNode (node, 21); // non-intersection segment L1 = new ListNode (L1, 12); L1 = new ListNode (L1, 13 ); l2 = new ListNode (L2, 22 ); L2 = new ListNode (L2, 23); L2 = new ListNode (L2, 24); L2 = new ListNode (L2, 25);} void main () {testCross (); // int length1 = getListLength (L1); // cout <length1 <endl; // ListNode * node = getThatListNode (L1, 3 ); // cout <node-> data <endl; ListNode * node = getNoCircleListCrossNode (L1, L2); if (node! = NULL) cout <node-> data <endl; elsecout <"no intersection" <endl; system ("pause ");}

The premise of the previous two discussions is that the linked list has no loops, but what if the linked list has loops? Next, chat.

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.