C Language Enhancement (7) linked list intersection question _ 1 judge the intersection of a non-circular linked list, intersection _ 1

Source: Internet
Author: User

C Language Enhancement (7) linked list intersection question _ 1 judge the intersection of a non-circular linked list, intersection _ 1

This blog post explains an ancient intersection of linked lists in five articles.


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

This article starts with the simplest way to determine whether two [no-ring] linked lists are intersecting. By the way, we will introduce the basic knowledge of the linked list to help some people who do not know much about the linked list.
Basic knowledge
What is a linked list?A linked list is a non-sequential storage structure on a physical storage unit. The logical sequence of data elements is achieved through the pointer links in the linked list. A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime. Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address.
-- Draw a graph From each ke to show the linked list
The data structure is as follows:
struct ListNode{int data;ListNode * nextNode;ListNode(ListNode * node,int value){nextNode=node;data=value;}};

Is there a chain table?You only need to modify the pointer to find that the linked list will never come to an end, as shown below:

How can I determine whether two linked lists overlap?Idea: as long as one node is the same, the two linked lists will interwork. Method 1: traverseIn the traversal chain table 1, each time it traverses a node in the Linked List 1, it determines whether it is the same as the node in the Linked List 2 (the method is also traversal). If it is the same, the two linked lists are intersecting. This method is feasible, but time complexity = O (length1 * leng22)
Method 2: Hash Table Method

Since the same memory address must exist at the intersection of linked lists, and the memory address of different nodes must be different, you can use the memory address to create a hash table, in this way, we can judge whether two linked lists have nodes with the same memory address. The specific method is to traverse the first linked list, create a hash table using the address, and traverse the second linked list, check whether the address hash value is the same as the node address value in the first table to determine whether the two linked lists are intersecting.
Time complexity O (length1 + leng22)
Space complexity O (length1) because a hash table with the size of length1 needs to be created

Analysis: time complexity is linear, acceptable, and the first intersection node can be found by the way, but it increases the space complexity of O (length1), which is obviously not satisfactory. -- Ref: http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2580026.html


Method 3: Compare the End Node

As long as the two linked lists intersect, the segment after the intersection must be the same, that means the end node is the same

Time complexity O (length1 + leng22)

Space complexity O (0)


The function for finding the end node is very simple and will not be explained.

/** Find the end node */ListNode * getLastNode (ListNode * head) {if (head = NULL) return NULL; while (head-> nextNode! = NULL) {head = head-> nextNode;} return head ;}


Source code

# Include <stdio. h> # include <stdlib. h> # include <iostream> using namespace std;/** 1. determine whether two [no-ring] linked lists overlap ideas determine whether the tail node is equal * // ** linked list struct */struct ListNode {int data; ListNode * nextNode; ListNode (ListNode * node, int value) {nextNode = node; data = value ;}}; ListNode * L1; ListNode * L2; // The void ScanList (ListNode * node) {while (NULL! = Node) {cout <node-> data <endl; node = node-> nextNode;}/** find the end node */ListNode * getLastNode (ListNode * head) {if (head = NULL) return NULL; while (head-> nextNode! = NULL) {head = head-> nextNode;} return head;} // test 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 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);} void main () {testCross (); // testNotCross (); ListNode * node1 = getLastNode (L1); ListNode * node2 = getLastNode (L2 ); if (node1 = node2) cout <"intersection" <endl; elsecout <"not intersection" <endl; system ("pause ");}

Now that we know the intersection of two [non-ring] linked lists, how can we find the intersection Node? In the next section, let's talk about it.







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.