Algorithm of Hunter learning-determining the intersection of two linked lists on a node

Source: Internet
Author: User

Problem:

  • How can I determine whether two linked lists are at a node?

Analysis:

  • Drawings describe the scenario when linked lists are intersecting!
  • Discover the differences and features of intersection and not intersection!

Conclusion:

  • The intersection linked list is found, and the nodes behind the intersection are the same. through simplification, we can conclude that the last node must be the same node.

//============================================================================// Name        : LinedListCrossing.cpp// Author      : jue// Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include <stdio.h>#include <stdlib.h>typedef struct Node{struct Node *next;int data;}LinkedList;bool isLinkedListCossing(LinkedList *l1,LinkedList *l2){Node *node1 = l1;while(node1->next){node1 = node1->next;}Node *node2 = l2;while(node2->next){node2 = node2->next;}return node2 == node1;}int main(){LinkedList* l1 = (LinkedList*)malloc(sizeof(LinkedList*));LinkedList* l2 = (LinkedList*)malloc(sizeof(LinkedList*));Node* node_a = (Node*)malloc(sizeof(Node*));Node* node_b = (Node*)malloc(sizeof(Node*));Node* node_c = (Node*)malloc(sizeof(Node*));node_a->data = 1;node_a->next = node_b;node_b->data = 2;node_b->next = node_c;node_c->data = 3;node_c->next = NULL;//l1->next = node_a;//l1->data = 0;l2->next = node_b;l2->data = 0;bool isTrue = isLinkedListCossing(l1,l2);printf("is cossring = %s",isTrue ? "true" : "false");return 0;}

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.