473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list question

Hi,

Is there any efficient way of finding the intesection point of two
singly linked lists ?
I mean to say that, there are two singly linked lists and they meet at
some point. I want to find out the addres of the node where the two
linked intersect.

thanks for any help...

Jan 22 '08
46 3432
user923005 <dc*****@connx. comwrites:
Consider:
listA: A -- B -- C -- D J -- K
\ /
E -- F -- G
/ \
listB: H -- I |
\ L
N -- M -- /
I don't see how this can happen in a linked list structure. A
linked list node can have only one successor, but your nodes I
and G appear to each have two (E and N, and J and L,
respectively).
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Jan 22 '08 #21
Richard Heathfield wrote:
>
.... snip ...
>
Indeed. Consider these linked lists:

listA: A -- B -- C -- D
\
E -- F -- G
/
listB: H -- I

You compare A with H, B with I, C with E, D with F, E with G.
Now the loop stops, and q is NULL. You missed the merge point
completely.

I believe this has to be done in two nested loops (or at least,
"as if" it were two nested loops! - i.e. O(listAnodecoun t *
listBnodecount) ), but I'd be delighted to be proved wrong.
You can scan through the lists. Do so, retaining L as the length
of listA, and L1 as the length of listB. Now reverse listA in
place, and remeasure the length of list B, getting L2.

In the example, L is 7, L1 is 5, L2 is 7. I think you can now find
the location of item E in either list.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Jan 22 '08 #22
On 22 Jan, 09:42, Richard Heathfield <r...@see.sig.i nvalidwrote:
Ravishankar S said:

<snip>


you mean point of merge of linked list ? i cannot imaging "intersecti on"
of singly linked
p *= listA;
q = *listB;
while(p && q) {
* * if(p->next == q->next) break;
* * p = p->next;
* * q = q->next;
}
if(p && q && (p->next == q->next))
{
* * printf("Merging point found\n");
}
But careful: I have not tested the code !

Indeed. Consider these linked lists:

listA: A -- B -- C -- D
* * * * * * * * * * * *\
* * * * * * * * * * * * E -- F -- G
* * * * * * * * * * * */
listB: * * * * * H -- I

You compare A with H, B with I, C with E, D with F, E with G. Now the loop
stops, and q is NULL. You missed the merge point completely.

I believe this has to be done in two nested loops (or at least, "as if" it
were two nested loops! - i.e. O(listAnodecoun t * listBnodecount) ), but I'd
be delighted to be proved wrong.
How about - you measure the length of the lists, subtract one from the
other, and step that number of steps into the longer list. Now step
through each list simultaneously, looking for a match.

Paul.
Jan 22 '08 #23
gw7...@aol.com wrote:
Richard Heathfield <r...@see.sig.i nvalidwrote:
... Consider these linked lists:
listA: A -- B -- C -- D
* * * * * * * * * * * *\
* * * * * * * * * * * * E -- F -- G
* * * * * * * * * * * */
listB: * * * * * H -- I
<snip>
I believe this has to be done in two nested loops (or at
least, "as if" it were two nested loops! - i.e.
O(listAnodecoun t * listBnodecount) ), but I'd
be delighted to be proved wrong.

How about - you measure the length of the lists, subtract
one from the other, and step that number of steps into the
longer list. Now step through each list simultaneously,
looking for a match.
More formally...

Declare the following variables

LA := length of list A
LB := length of list B
LB' := length of list B if list A is reversed

DA := number of leading nodes distinct to A
DB := number of leading nodes distinct to B
CAB := number of nodes common to A and B

From observation, we have...

LA = DA + CAB
LB = DB + CAB
LB' = DB + DA + 1

Rewritten as...

DA + CAB = LA
DB + CAB = LB
DA + DB = LB' - 1

Which is solved by...

CAB := (LB - LB' + LA + 1)/2
DA := LA - CAB
DB := LB - CAB

The algorithm (including restoration of A from G) is left
as an exercise, but it should be clear that since reverse
and length count are O(N), and we perform a fixed number
of steps of each, the algorithm is O(N + M).

Or I may have forgotten to carry 1 and I'm talking bollocks. ;)

--
Peter
Jan 23 '08 #24
On Jan 22, 2:40*pm, Ben Pfaff <b...@cs.stanfo rd.eduwrote:
user923005 <dcor...@connx. comwrites:
Consider:
*listA: A -- B -- C -- D * * * * * * J -- K
* * * * * * * * * * * * \ * * * * * /
* * * * * * * * * * * * *E -- F -- G
* * * * * * * * * * * * / * * * * * \
*listB: * * * * * H -- I * * * * * * |
* * * * * * * * * * * * \ * * * * * *L
* * * * * * * * * * * * * N -- M -- /

I don't see how this can happen in a linked list structure. *A
linked list node can have only one successor, but your nodes I
and G appear to each have two (E and N, and J and L,
respectively).
listA has nodes A,B,C,D,E,F,G,L ,N,M,I
listB has nodes H,I,E,F,G,J,K
They share I,E,F,G
Jan 23 '08 #25
On Jan 22, 9:42*am, Richard Heathfield <r...@see.sig.i nvalidwrote:
Indeed. Consider these linked lists:

listA: A -- B -- C -- D
* * * * * * * * * * * *\
* * * * * * * * * * * * E -- F -- G
* * * * * * * * * * * */
listB: * * * * * H -- I

You compare A with H, B with I, C with E, D with F, E with G. Now the loop
stops, and q is NULL. You missed the merge point completely.

I believe this has to be done in two nested loops (or at least, "as if" it
were two nested loops! - i.e. O(listAnodecoun t * listBnodecount) ), but I'd
be delighted to be proved wrong.
You can do it easily with three loops, not nested. First follow the
linked list from A to the end, finding that the first linked list has
seven elements and ends at G. Then follow the second linked list to
find it has five elements and also ends in G. Since both lists end
with G, they must merge at some point (if they end with different
notes, they definitely don't merge). Since the first list has two
elements more, skip two elements, then go through the lists starting
at C and H and iterate until they meet.
Jan 23 '08 #26
user923005 <dcor...@connx. comwrote:
Ben Pfaff <b...@cs.stanfo rd.eduwrote:
user923005 <dcor...@connx. comwrites:
Consider:
*listA: A -- B -- C -- D * * * * * * J -- K
* * * * * * * * * * * * \ * * * * * /
* * * * * * * * * * * * *E -- F -- G
* * * * * * * * * * * * / * * * * * \
*listB: * * * * * H -- I * * * * * * |
* * * * * * * * * * * * \ * * * * * *L
* * * * * * * * * * * * * N -- M -- /
I don't see how this can happen in a linked list structure.
*A linked list node can have only one successor, but your
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
nodes I and G appear to each have two (E and N, and J and
L, respectively).

listA has nodes A,B,C,D,E,F,G,L ,N,M,I
listB has nodes H,I,E,F,G,J,K
They share I,E,F,G
How is G's next link to both L _and_ J?

--
Peter
Jan 23 '08 #27
user923005 wrote:
>
.... snip ...
>
Consider:
listA: A -- B -- C -- D J -- K
\ /
E -- F -- G
/ \
listB: H -- I |
\ L
N -- M -- /

Or even:
listA: C -- D J -- K
\ /
E -- F -- G
/ \
listB: H -- I L

Graph problems have a nasty habit of turning out harder than they
appear at first glance.

If they can itersect, then they can probably also diverge and cycle.
If you don't test for it, funny things can happen, but not "ha-ha"
funny.
Those can't happen. He originally specified singly linked lists.
You have to be able to form and manipulate them from:

struct node {
struct data *datum;
struct node *next;
}

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 23 '08 #28
On Jan 22, 5:00*pm, Peter Nilsson <ai...@acay.com .auwrote:
user923005 <dcor...@connx. comwrote:
Ben Pfaff <b...@cs.stanfo rd.eduwrote:
user923005 <dcor...@connx. comwrites:
Consider:
*listA: A -- B -- C -- D * * * * * * J -- K
* * * * * * * * * * * * \ * * * * * /
* * * * * * * * * * * * *E -- F -- G
* * * * * * * * * * * * / * * * * * \
*listB: * * * * * H -- I * * * * * * |
* * * * * * * * * * * * \ * * * * * *L
* * * * * * * * * * * * * N -- M -- /
I don't see how this can happen in a linked list structure.
>*A linked list node can have only one successor, but your

* * ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
nodes I and G appear to each have two (E and N, and J and
L, respectively).
listA has nodes A,B,C,D,E,F,G,L ,N,M,I
listB has nodes H,I,E,F,G,J,K
They share I,E,F,G

How is G's next link to both L _and_ J?
listA has nodes A,B,C,D,E,F,G,L ,N,M,I
G's next link is L.

listB has nodes H,I,E,F,G,J,K
G's next link is J.

The picture above is the actual node structure, showing what is held
in common, if the graphs were actually merged.
I was trying to point out that if we are given listA and listB as
above, then the simple "find the node where they meet" strategy is not
going ot produce expected results. If you have some sort of merge
operation, all sorts of strange things might occur.
Jan 23 '08 #29
On Jan 22, 3:19*pm, CBFalconer <cbfalco...@yah oo.comwrote:
user923005 wrote:

... snip ...
Consider:
*listA: A -- B -- C -- D * * * * * * J -- K
* * * * * * * * * * * * \ * * * * * /
* * * * * * * * * * * * *E -- F -- G
* * * * * * * * * * * * / * * * * * \
*listB: * * * * * H -- I * * * * * * |
* * * * * * * * * * * * \ * * * * * *L
* * * * * * * * * * * * * N -- M -- /
Or even:
*listA: * * * * * C -- D * * * * * * J -- K
* * * * * * * * * * * * \ * * * * * /
* * * * * * * * * * * * *E -- F -- G
* * * * * * * * * * * * / * * * * * \
*listB: * * * * * H -- I * * * * * * L
Graph problems have a nasty habit of turning out harder than they
appear at first glance.
If they can itersect, then they can probably also diverge and cycle.
If you don't test for it, funny things can happen, but not "ha-ha"
funny.

Those can't happen. *He originally specified singly linked lists.
You have to be able to form and manipulate them from:

* * struct node {
* * * *struct data *datum;
* * * *struct node *next;
* * }
listA has nodes A,B,C,D,E,F,G,L ,N,M,I
listB has nodes H,I,E,F,G,J,K
If the lists could be merged, the "afterwards picture" showing the
common parts of the graph would look like my picture.

I was showing that if (after we find the first common node) all the
rest of the nodes are not identical from that point forward, then the
simple y sort of structure is not going to correctly model the data.
Jan 23 '08 #30

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

Similar topics

10
15137
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
19
13579
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
7
2616
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
12
15102
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition should be kept for references to previous element of list. Here is my solution below: struct node {
57
4310
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
1498
by: Paminu | last post by:
I have a Linked-List and would like to create pointers to elements in this list. e.g I would like two pointers that point to each of their elements in the Linked-List. But there should always be exactly 5 nodes between these pointers. Does this make any sense or are there some more efficient way to access certain elements in a Linked-List?
12
3955
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
9
2846
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node * nPtr; //the next node's pointer }
2
1702
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of all, I am using C++, however it's managed C++ or visual C++, or whatever microsoft calls it. I'm using MSVS2005, and working on a Windows Forms Application project from the C++ projects tab. I'm pointing this out because apparently the syntax used in...
11
2556
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9177
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.