C language Enhancement (vii) the intersection problem of linked list _1 to judge the intersection of non-ring linked list

Source: Internet
Author: User

Starting from this post, we explain an ancient list of five intersecting issues


Topic

Give the head pointers of two unidirectional lists, such as H1,H2, to determine if the two lists intersect


Problem solving steps

    1. Determine if two "ring-free" linked lists intersect
    2. Found intersection nodes of two "ring-free" linked lists
    3. Determine if a linked list has a ring
    4. Determine if two "ring" linked lists intersect
    5. Found intersection nodes of two "ring" linked lists
This article first from the simplest judge two "no ring" linked list whether the intersection began , by the way, introduce the basic knowledge of the list, convenient for some of the list of students who do not know much about learning.
Basic Knowledge
What is a linked list? list is a physical storage unit storage structure data element pointer The link order is implemented. A linked list consists of a series of nodes (each element in the list is called a node) that can be dynamically generated at run time. Each node consists of two parts: one is to store the data element pointer field.
--from Baikedraw a diagram to show the list
data structures are as follows
struct Listnode{int data; ListNode * NEXTNODE; ListNode (ListNode * node,int value) {nextnode=node;data=value;}};

a linked list? just modify the pointer pointing, you will find that this list will never come to an end, as follows

How can I tell if two linked lists intersect? idea: As long as one node is the same, then two linked lists intersect method One: Traverse traversaliterate through the chain table one, each time traversing to a node of the linked list is the same as the node in the list two (the method is also traversed), the same as the two linked lists intersect. This method is certainly feasible, but time complexity =o (length1*length2)
method Two: Hash table method

Since even a linked list once intersect, the intersection node must have the same memory address, and different node memory address must be different, then you may wish to use memory address to establish a hash table, so by judging whether there are two linked list memory address of the same node to determine whether the two linked list intersect. This is done by traversing the first linked list and using the address to set up a hash table, traversing the second list to see if the address hash is the same as the node address value in the first table to determine if the two linked lists intersect.
time Complexity O (length1 + length2)
space Complexity O (length1) because you need to create a hash table of size length1

Analysis: Time complexity is linear, acceptable, and can be found in the way to the first intersection node, but increases the space complexity of O (length1), which is obviously unsatisfactory. --ref:http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2580026.html


Method Three: Compare the tail knot point

As long as the two linked lists Intersect, then the section after the intersection must be the same, it means the tail node is the same

Time complexity O (length1 + length2)

space Complexity O (0)


Look for the function of the tail node, very simple, do not explain

/** Find Tail Node */listnode * Getlastnode (ListNode * head) {if (head==null) return Null;while (head->nextnode!=null) {head= Head->nextnode;} return head;}


Source

#include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;/**1. Judging two "no rings" Whether the linked list intersects the idea of determining whether the tail node is equal *//** the linked list structure */struct listnode{int data; ListNode * NEXTNODE; ListNode (ListNode * node,int value) {nextnode=node;data=value;}}; ListNode * L1; ListNode * l2;//traversal list void scanlist (ListNode * node) {while (Null!=node) {Cout<<node->data<<endl;node = Node->nextnode;}} /** Find Tail Node */listnode * Getlastnode (ListNode * head) {if (head==null) return Null;while (head->nextnode!=null) {head= Head->nextnode;} return head;} Test loop-free intersection void Testcross () {ListNode * node = new ListNode (null,0); node = new ListNode (node,1); node = new ListNode (node,2); L1 = new ListNode (node,11); L1 = new ListNode (l1,12); L1 = new ListNode (l1,13); L2 = new ListNode (node,21); L2 = new ListNode (l2,22); L2 = new ListNode (l2,23);} Test loop-free disjoint void Testnotcross () {L1 = new ListNode (null,11); L1 = new ListNode (l1,12); L1 = new ListNode (l1,13); L2 = new ListNode (null,21); L2 = new ListNode (l2,22); L2 = new ListNode (l2,23);} voidMain () {Testcross ();//testnotcross (); ListNode * Node1 = Getlastnode (L1); ListNode * Node2 = Getlastnode (L2), if (node1==node2) cout<< "intersect" <<endl;elsecout<< "disjoint" <<endl; System ("Pause");}

now that you know two "no ring" linked lists intersect, then how to find the intersection node, the next section, chat about this.







C language Enhancement (vii) the intersection problem of linked list _1 to judge the intersection of non-ring linked list

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.