In the task, you need to find the point of merging of simply connected lists and display the value in this node. I made it very simple: if the lists are of different lengths, I calculate the difference of their lengths, this is the number of redundant elements. I skip the extra elements and start moving along the two lists at the same time until a common point is found.

Is it possible to solve the problem by inverting lists? I on a piece of paper drew various variants of inverting one list or two at once, considered a variant of passage from the end, but nothing came to mind.

Is there any segolts in my code?

/* Find merge point of two linked lists Node is defined as struct Node { int data; Node* next; } */ // ΠŸΠΎΠ΄ΡΡ‡Π΅Ρ‚ количСства элСмСнтов Π² спискС int count(Node *head) { int c = 0; while(head) { c++; head = head->next; } return c; } int FindMergeNode(Node *headA, Node *headB) { if(headA && headB) // Если ΠΎΠ΄ΠΈΠ½ ΠΈΠ· списков Π½Π΅ пуст { int len_a = count(headA); // ВычислСниС Π΄Π»ΠΈΠ½ списков A ΠΈ B int len_b = count(headB); int diff = std::abs(len_a - len_b); // Π Π°Π·Π½ΠΎΡΡ‚ΡŒ ΠΌΠ΅ΠΆΠ΄Ρƒ Π΄Π»ΠΈΠ½Π°ΠΌΠΈ списков for(int i = 0; i < diff; i++) // ΠŸΡ€ΠΎΠΏΡƒΡΠΊ Π»ΠΈΡˆΠ½ΠΈΡ… элСмСнтов Π² самом Π΄Π»ΠΈΠ½Π½ΠΎΠΌ спискС (len_a > len_b) ? headA = headA->next : headB = headB->next; while(headA != NULL && headB != NULL) { if(headA == headB) // Если Π½Π°ΠΉΠ΄Π΅Π½Π° Ρ‚ΠΎΡ‡ΠΊΠ° слияния (адрСса Π½ΠΎΠ΄ Ρ€Π°Π²Π½Ρ‹) return headA->data; headA = headA->next; headB = headB->next; } } return 0xDEADBEEF; } 

enter image description here

  • Describe the problem in question in Russian words . Do not force to run on the links. - avp
  • Task: two lists merge at some point. Need to find this point. Added an example in the post. - typemoon
  • Obviously, the task has a trivial solution if you expand one of the lists. But who said that you can deploy from? Usually the Find verb implies non-mutated algorithms. - Abyx
  • No wonder someone minus you. After describing the task, state the short algorithm (idea and underlying assumptions about the contents of the lists) to the code (in Russian words) and comment out the code. - avp
  • @avp, a verbal description of the algorithm, starting with the second sentence, is sufficient. Komenty added. - typemoon

1 answer 1

What is the point of the question if the problem is solved? Hackerrank provides testcase's. Once she passes them, what else do you want? If you solve problems there, you should know that if a segfolt is fundamental in it (the task) and you try to pass the testcase with it, you will receive a corresponding message. Also, if a task is solved in time for too long, you will not pass the testcase's either until you optimize it. Why are you stuck in a trivial task with a simple level of complexity? Decided - go on. There you are waiting for much more interesting things.

  • I want to solve it in a non-trivial way. And I really dwell on many things, I don’t know why this is happening, but there is a strong desire to do something else or study something deeper, even if there is no sense in the knowledge gained. - typemoon
  • @typemoon Believe me, you will find there interesting things in abundance. Sit you also think over some tasks for more than one day. And reinvent the wheel does not make sense. Invent at least a bicycle :) - Max ZS