Determines whether the two linked lists intersect if the intersection finds the first point of intersection

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>typedef struct linknode{struct linknode* next; int data;}    linklist;/* Description: The single-linked list of all the leading nodes *//* create a linked list */void createlinklist (linklist* head, int* A, int n) {int i = 0;    linknode* node = NULL;        while (I < n) {node = new Linknode;        Node->next = head->next;        Node->data = A[i];        Head->next = node;    i++;   }}/* gets the length of the list */int getlenoflist (linklist* head) {int i = 0;      linknode* node = head;        while (node->next! = NULL) {i++;   node = node->next; } return i;}        /* Get the list of K nodes */linknode* Getknode (linklist* head, int K) {linknode* p = head;        while ((P->next!=null) && (k>0)) {p = p->next;    k--; } return (k>0)? Null:p;} /* Note that this function will find a linked list without a ring, if the band needs to reconsider *//* reference:*//* Note:   To change the blog should be omitted there is also a situation in the case of looping, if it is handled as follows: Linked list a:o->o->o->o->o--> ^ ^ |     |     | |      |   ------|   | <----O<-o<-o<-o: The list B method is actually very similar: to determine whether a ring, find looping node is the same, if the same, then according to the judgment, take two linked list length, let long first go n-m (N,m are two linked list length) and then go forward , if the node is equal and not the ring node exits, find the first junction of the intersection *//* Description: The change function can continue to optimize, if you do not want to directly determine whether the last node is equal exit */bool Findfirstcommonnode (linklist* l, Linklist*s, linknode* &node) {int n = getlenoflist (l);/* Gets the list length */int m = getlenoflist (s);/* Gets the list length */Linknode    * p;        linknode* Q;       /* Find the starting point */if (n > m) {p = Getknode (l, n-m) respectively;    Q = s;        } else {p = Getknode (S, m-n);    Q = l;            }/* Go down to find the starting point */while (P!=null && q!=null) {if (p = = q) {node = p;        return true;        } p = p->next;    Q = q->next; } return false;}      /* Add the S-linked footer node to the K-position of the L-linked list */bool Appendknode (linklist* L, linklist* s, int K) {linknode* lastnode = s;            linknode* node = l;      while (lastnode->next! = NULL) {lastnode = lastnode->next; } while (Node-> next!=null && (k>0) {node = node->next;      k--;      } if (K > 0) {return false;      } lastnode->next = node; return true;}    /* Print List */void printlinklist (linklist* head) {linknode* node = head->next;        while (node! = NULL) {printf ("%d\t", node->data);    node = node->next; } printf ("\ n");}    int main () {/* Creates a linked list */linklist L = {0};    linknode* node = NULL;    int a[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    Createlinklist (&l, A, 9);        Printlinklist (&AMP;L);    /* Create a linked list */linklist s = {0};    int b[] = {15, 14, 13, 12};    Createlinklist (&s, B, 4);        Printlinklist (&s);    Findfirstcommonnode (&l, &s, node);    if (node! = NULL) {printf ("First common node%d\n", Node->data);    } else {printf ("NO COMMON node\n");    }/* Adds an S-linked footer node to the K-position of the L-linked list */Appendknode (&l, &s, 3);    Printlinklist (&s); FindfirstcommonNode (&l, &s, node);    if (node! = NULL) {printf ("First common node%d\n", Node->data); } return 0;}

Determines whether the two linked lists intersect if the intersection finds the first point of intersection

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: 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.