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;}