Interview C language title

L1, L2 - 2 signle linked list , Null terminate.
p1, p2 are pointer to L1 ,L2


describe an effieicnt way to retrive the first common node between L1 ,L2.

list examples:

      p1
      []->[0]->[6]->[8]
                       \
                       [4]->[10]->[11]->NULL
                      /
          [1]->[2]->[3]
          p2


  lenght1 = get_list_length(L1);
  lenght2 = get_list_length(L2);
    ...


<< OR >>

p1
[]->[]->[]->[]->[]->NULL


[]->[]->[]->[]->[]->NULL
p2

--------------------
I want to travel p1 link list, then write the address of every node of p1 link list.
For example, I record this in p1_address[64][64];
Then the same to P2 link list. the address of every node of p2 link list in p2_address[64][64];

Then I compare p1_address, p2_address.

time complexity of you suggestion -
o(n) ?
o(n^2) ?

O(n^2),

struct node {
...
char common; <---add this field
...
}

when p1 travel p1 link list, p1 will check this common. If the common is 0, then p1 set common to 1.
Then p2 travel p2 link list, p2 will check this common, too. If this common is 1, p2 will find the first
common node.

Is this solution better? It only add a new field.

p1 + p2 --->p3

p2 + p1 --->p4

then p3 and p4 have the same size.
then travel p3, p4, compare

list length ? how you get it ?
travel list p1, then travel list p2,


===================== code it ... define the functions C syntax. define the node struct (next)....
code!

struct node {
    int data;
    struct node *next;
};
struct node *p1;
struct node *p2;

int get_list_length(struct node *p)
{
    int length = 0;

    while (p != NULL) {
        length++;
        p = p->next;
    }

    return length;
}

//the same size
struct node *same_size_common(struct node *ptr1, struct node *ptr2)
{
    while (ptr1 != NULL) {
            if (ptr1 != ptr2) {
                ptr1 = ptr1->next;
                ptr2 = ptr2->next;
            } else {
                return ptr1;
            }
    }

    //no common node
    return NULL;
}

//In this function, p1_length is bigger than p2_length
struct node *not_same_comp(int p1_length, int p2_length, struct node *ptr1, struct node *ptr2)
{

    int i, delta = p1_length - p2_length;

    for (i=0; i<delta; i++) {
        if (ptr1 == ptr2)
              return ptr1; // This means the ptr2 is the common node.

        ptr1 = ptr1->next;
    }

    //the common node is in the same part
    return same_size_common(ptr1, ptr2);
}

struct node *find_early_common(struct node *p1, struct node *p2)
{
    int p1_length, p2_length;
    struct node *ptr1 = p1;
    struct node *ptr2 = p2;

    p1_length = get_list_length(ptr1);
    p2_length = get_list_length(ptr2);

    if (p1_lengh == p2_length){
        return same_size_common(ptr1, ptr2);
    }

    //ptr1 is bigger than ptr2
    if (p1_length > p2_length) {
        return not_same_comp(p1_length, p2_length, ptr1, ptr2);
    }

    //ptr2 is bigger than ptr1
    if (p1_length < p2_length) {
        return not_same_comp(p2_length, p1_length, ptr2, ptr1);
    }
}

Published 234 original articles · won praise 12 · views 240 000 +

Guess you like

Origin blog.csdn.net/mounter625/article/details/103327644