473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Find the node where 2 lists merge

Hello all,

I have a data structures problem.
Assume there are 2 linked lists L1 and L2. They merge into some node.

L1 1->2->3->4->5->6
/
L2 8->7->9->/

L1 and L2 merge in node with value 5. We need to return the pointer to
the node with value 5.
What is the best and efficient solution to find the node where the 2
lists merge?

Thanks,
Anunay

Apr 21 '06 #1
9 3006
an*********@gmail.com said:
Hello all,

I have a data structures problem.
Assume there are 2 linked lists L1 and L2. They merge into some node.

L1 1->2->3->4->5->6
/
L2 8->7->9->/

L1 and L2 merge in node with value 5. We need to return the pointer to
the node with value 5.
What is the best and efficient solution to find the node where the 2
lists merge?


mergenode = 0;
while(l1 && l2 && mergenode == 0)
{
++l1->count;
++l2->count;
if(l1->count == 2)
{
mergenode = 1; /* l1 points to node at merge point */
}
else if(l2->count == 2)
{
mergenode = 2; /* l2 points to node at merge point */
}
else
{
l1 = l1->next;
l2 = l2->next;
}
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 21 '06 #2
Hi Richard,

Can you please explain me the logic of the above code?

Thanks,
Anunay

Apr 26 '06 #3
an*********@gmail.com opined:
Hi Richard,
Richard who?
Can you please explain me the logic of the above code?


There's no code above.

Please quote context (what you're replying to, and who said it). Read
the link in my sig.

--
"Oh, I've seen copies [of Linux Journal] around the terminal room at
The Labs."
(By Dennis Ritchie)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 26 '06 #4
Vladimir Oka opined:
an*********@gmail.com opined:
Hi Richard,


Richard who?


Ooops.
Apologies.
Didn't think it'd sound like this. :-(

--
"Computers may be stupid, but they're always obedient. Well, almost
always."

-- Larry Wall (Open Sources, 1999 O'Reilly and Associates)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 26 '06 #5
[Sorry to reply to my own article, but an explanation of the logic of my
code has been requested. First, the problem statement (abbrev'd).]

Richard Heathfield said:
an*********@gmail.com said:
What is the best and efficient solution to find the node where the 2
lists merge?


We will have two pointers, and start them off at the beginnings of the two
branches. If the branches merge, we need a pointer to one or other of them.

We begin by assuming that each list is terminated by a null pointer, and
that every node on each list has an int field, count, which is initially
set to 0. If you like, think of it as being unpainted. If, during our
travels, our first pointer hits it, we'll add 1 to it (paint it blue). If
our second pointer hits it, we'll add 1 to it (paint it yellow). If, after
such an addition, the count value is 2 (painted blue+yellow=green), we have
just been processing a node that is in both lists. Since we have not yet
encountered such a node, this must be the first merge point.

So imagine two paintbrushes, each one travelling down its own branch of the
list, one painting a blue stripe and the other painting a yellow stripe. As
soon as we detect green, we stop, and our paintbrush is hovering right over
the merge point.
Here is the code again:

mergenode = 0;
while(l1 && l2 && mergenode == 0)
{
++l1->count;
++l2->count;
if(l1->count == 2)
{
mergenode = 1; /* l1 points to node at merge point */
}
else if(l2->count == 2)
{
mergenode = 2; /* l2 points to node at merge point */
}
else
{
l1 = l1->next;
l2 = l2->next;
}
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Apr 26 '06 #6
Here's another solution that came to mind..
Not exactly very efficient, but i find it easy to think recursively...
struct node * commonnode( struct node * ptr1, struct node * ptr2)
{

struct node * common;

if (ptr1==ptr2)
/* Common node found.. This may be null too.. that meas actually no
common node */
return ptr1;

if (!ptr1 || !ptr2) return NULL ; /* If one of them is null, no chance
of having a common node*/

common = commonnode(ptr1->next, ptr2);
if (common) /* Some common node found */
return common;

return commonnode(ptr1,ptr2->next);

}

Apr 26 '06 #7
>L1 1->2->3->4->5->6
/
L2 8->7->9->/ L1 and L2 merge in node with value 5. We need to return the pointer to
the node with value 5.
What is the best and efficient solution to find the node where the 2
lists merge?


one more solution is possible if the linked lists are read only and we
can't have a counter
traverse through L1..let its length be m+l (l is the length of common
part of the linked list)
similarly L2 has length n+l
subtract both you will get m-n
if it is positive L1 has m-n nodes more than L2
traverse these many nodes on L1.
now L1 and L2 has same number of nodes till end.
go on incrementing and comparing the nodes , you will get the point
where both the lists are merging

regards
jitendra.

Apr 26 '06 #8
Amit Bhatnagar wrote:

Here's another solution that came to mind..
Not exactly very efficient, but i find it easy to think recursively...


You have already been told that you need to include context.
Without it your article is simply meaningless blather to most
readers. Google is not usenet, so act accordingly. See below for
means.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Apr 26 '06 #9
I could think of a better solution...
Its complexity is: O(length of longer list)
The funda is :
if lengths of both lists are equal, then we can just keep on moving
the pointers ptr1 and ptr2 and comparing them each time till we reach
a common node or null.

If the lengths are unequal, reduce the problem in terms of comparing
the list with equal number of nodes by moving the pointer to the longer
list till the number of nodes in the resulting lists are equal..

Here's some code for above logic:
struct node * commonnode( struct node * ptr1, struct node * ptr2)
{
int l1,l2;
l1=listlength(ptr1), ;
l2=listlength(ptr2);
/* calculating Length of a list is trivial; can be done in O(n) time
if n nodes are there*/

for (int i=0;i< l1-l2;i++)
ptr1=ptr1->next;

for (int i=0;i< l2-l1;i++)
ptr2=ptr2->next;
/* Moving the pointer to longer list till the length of remaining list
equals thatt of shorter list; above loops have no effect on shorter
list */

/* Now we have equal nodes in the lists pointed to by ptr1 and ptr2*/

while (ptr1 && ptr2) /*Continue as long as one of them is null*/
{
if (ptr1==ptr2)
return ptr1; /* Common node found.. */

ptr1=ptr1->next;
ptr2=ptr2->next;
}

/* No common node :-( Return NULL */
return NULL;
}

Apr 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Alan Little | last post by:
I need to merge and de-duplicate some lists, and I have some code which works but doesn't seem particularly elegant. I was wondering if somebody could point me at a cleaner way to do it. Here's...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
7
by: deanfamily | last post by:
Here's the problem: I have two linked lists of integers (list1 and list2) that are already in sequential order, and I need to combine them into one list in sequential order (newList). Any...
21
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
6
by: Tekkaman | last post by:
I have a list of lists and I want to define an iterator (let's call that uniter) over all unique elements, in any order. For example, calling: sorted(uniter(, , ])) must return . I tried the...
9
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them...
14
by: etal | last post by:
Here's an algorithm question: How should I efficiently merge a collection of mostly similar lists, with different lengths and arbitrary contents, while eliminating duplicates and preserving order...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.