English 中文(简体)
C中合并的关联清单
原标题:merged linked list in C

This question was asked to me in an interview: There are two header of two linked lists. There is a merged linked list in c where in the second linked list is merged into the first one at some point. How could we identify the merging point and what is the complexity of finding that point ?

谁能提供帮助?

问题回答

O(n)

search = list1->header;
if (mixed->header == list1->header) search = list2->header;

while (mixed->next != search) mixed = mixed->next;

Edit: 变量的新名称和一些评论

/* search is what we want to find. Here it s the head of `list2` */
search = list2->header;
/* unless the merging put `list2` first; then we want to search for `list1` */
if (mixed->header == list2->header) search = list1->header;

/* assume (wrongly) that the header for the mixed list is the merge point */
mergepoint = mixed->head;

/* traverse the mixed list until we find the pointer we re searching */
while (mergepoint->next != search) mergepoint = mergepoint->next;
/* mergepoint now points to the merge point */

<>Update: 假设“Y-shaped”加入“Steve Jessop”哨所中描述的2个链接清单。 但我认为,对这一问题的描述非常模糊,可以作出各种解释,而这种解释只是一种解释。


可以通过单一通行证通过一个名单加上一个部分通过。 换言之,它是O(n)。

我提议的算法如下:

Create a hashmap. (Yes, this is busywork in C if you don t have a library handy for it). The keys will be pointers to the items in List1 (i.e. the head pointer and each link). The values will be integers denoting the position, i.e. distance from the head of List1.

通过名单1保持职位轨道,并公布所有你的协调人和职位。

通过名单2,追踪立场,并找到在地图上出现的第一点。

At this point, you ll know the position in List2 of the first node common to both lists. The hashmap entry will also contain the position in List1 of that same node. That will nicely identify your merge point.

如果问题指名单1(即名单2点在名单1的中间点)所载的名单2,那么就容易——只是行距清单1,并且比较点数,直到你到达名单2为止。

然而,这种解释并不有意义,因为通过将清单2列入清单1(如1 2 2 1),你也将修改名单2——最后1个成为清单2的一部分。

因此,我将假设:问题涉及Y form:

清单1:A->B->C->D->E->F

名单2:X->Y->Z->E->F

如卡尔所建议的那样,可以通过洗衣解决该问题。

解决办法,但不得有:

  1. Walk list1 and disconnect all its pointers as you go
  2. Walk list2. When it ends, you ve reached the junction point
  3. Repair the pointers in list1

清单1中的断线和修理点可以很容易地采用再入侵:

Diconnect(node)
{
    if (node->next == NULL)
      walk list2 to its end, that is the solution, remember it
    else
    {
       tmp = node->next;
       node->next = NULL;
       Disconnect(tmp);
       node->next = tmp;  // repair
    }
}

现在称为脱节(清单1)。

重新列出清单1 和断线点。 当你到场时,执行第2步(航海清单2)以找到关口,修理点员在从入侵返回时返回。

这一解决办法暂时修改了清单1,因此,它并不安全,你应该使用离散电话的锁。

合并法

   void mergeNode(){  
          node *copy,*current,*current1;

          free(copy);
      merge=NULL;
     current=head;
   current1=head1;
    while(current!=NULL){
    if(merge==NULL){
        node *tmp;
        tmp=(node*)malloc(sizeof(node));
        tmp->data=current->data;
        tmp->link=NULL;
        merge=tmp;
    iii
    else{
        copy=merge;
        while(copy->link!=NULL)
            copy=copy->link;
        node *tmp;
        tmp=(node*)malloc(sizeof(node));
        tmp->data=current->data;
        tmp->link=copy->link;
        copy->link=tmp;
    iii
    current=current->link;
iii
while(current1!=NULL){
    copy=merge;
    while(copy->link!=NULL)
        copy=copy->link;
    node *tmp;
    tmp=(node*)malloc(sizeof(node));
    tmp->data=current1->data;
    tmp->link=copy->link;
    copy->link=tmp;
    current1=current1->link;
iii
display(merge);

iii

如果我的回答过于简单,但如果你有两位联系的清单,由一位负责人确定,并加入其中,那么我会感到担忧。

A -> B-> C -> D是第一个清单,

1 -> 2 -> 3 -> 4为第二,然后提交

A -> B -> C -> 1 -> 2 -> 3 -> 4 -> D

然后,为了找到中间点,在找到第二位主人之前,你必须填写最后名单(1)。 在O(n1)最糟糕的情况下,N1是第一个清单的内容数目(如果第二个清单在最后合并,就会出现这种情况)。

这是我打算如何解决这个问题的。 提及C语可能意味着,除非具体指明,否则你没有目标或事先包装的数据结构。

如果上述两个清单的内容相同,那么我的解决办法就取得了一定的工作。 我怀疑,这是C语采取行动的:你可以寻找第二个名单第一部分(标题)的address。 因此,重复反对赢得了搁置。

解决这一问题的办法很多。

Note that i am only discussing the approaches[corner cases may need to be handled separately] starting from brute force to the best one.
Considering N: number of nodes in first linked list
M: number of nodes in second linked list

Approach 1:
Compare each node of first linked list with every other node of second list. Stop when you find a matching node, this is the merging point.

while(head1)
{
cur2=head2;
while(cur2)
{
    if(cur2==head1)
        return cur2;
    cur2=cur2->next;
}
head1=head1->next;
}

Time Complexity: O(N*M)
Space Complexity: O(1)

Approach 2:
Maintain two stacks. Push all the nodes of he first linked list to first stack. Repeat he same for second linked list.
Start popping nodes from both the stacks until both popped nodes do not match. The last matching node is the merging point.
Time Complexity: O(N+M)
Space Complexity: O(N+M)

Approach 3:
Make use of hash table. Insert all the nodes of the first linked list into hash.
Search for the first matching node of he second list in the hash.
This is the merging point.
Time Complexity: O(N+M)
Space Complexity: O(N)

Note that the space complexity may vary depending upon the hash function used[talking about C where you are supposed to implement your own hash function].

Approach 4:
Insert all the nodes of first linked list[by nodes, i mean addresses] into an array.
Sort the array with some stable sorting algorithm in O(N logN) time[Merge sort would be better].
Now search for the first matching node from the second linked list.
Time Complexity: O(N logN)
Space Complexity: O(N)

Note that this approach may be better than Approach 3 [in terms of space]as it doesn t use a hash.

Approach 5:
1. Take an array of size M+N.
2. Insert each node from the first linked list, followed by inserting each node from the second linked list.
3. Search for the first repeating element[can be found in one scan in O(M+N) time].
Time Complexity: O(N+M)
Space Complexity: O(N+M)

Approach 6: [A better approach]
1. Modify the first linked list & make it circular.
2. Now starting from the head of the second linked list, find the start of the loop using Floyd- Warshall cycle detection algorithm.
3. Remove the loop[can be easily removed as we know the last node].
Time Complexity: O(N+M)
Space Complexity: O(1)

Approach 7: [Probably the best one]
1. Count the number of nodes in first linked list[say c1].
2. Count the number of nodes in second linked list[say c2].
3. Find the difference[Lets say c1>c2] diff=c1-c2.
4. Take two pointers p1 & p2, p1 pointing to the head of the first linked list & p2 pointing to the head of the second linked list.
5. Move p1 diff times.
6. Move both p1 & p2 each node at a time until both point to the same node.
7. p1 or p2 indicates the merging point.
Time Complexity: O(N+M)
Space Complexity: O(1)

三方解决办法显然是O(N+M)。 Hm. 可以做得更好。 你可以从开始到名单上结束,反之亦然。 当你有线人时,你可以在一定时间走这些方向,这样就应该更快地成为排泄者。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签