IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

Data Structures -- Linked List -- Point Of Merging


varalu
no avatar
Super Member
*********
Group: Members
Posts: 468
Joined: 1-October 07
From: India
Member No.: 50,968
myCENT:1.17



Post #1 post Nov 13 2007, 10:35 AM
Given two singly linked lists with both of them merging at some point. Find the position at which they merge.

Eg:

1->30->50->65->2->59

88->46->65->2->59

The above two linked lists merge at 65.
Find this node where they merge..

Question Courtesy: Antony(Friend)


One possible Answer
A simple answer would be to find the length of both the linked lists and have two pointers, one for each linked list. Move (difference in lengths) steps in the longer linked lists and then start moving both the pointers by one every time until you have both the pointers pointing to the same node.

time Complexity - O(2n) = O(n)
Go to the top of the page
+Quote Post
fffanatics
no avatar
Privileged Member
*********
Group: Members
Posts: 936
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



Post #2 post Nov 14 2007, 05:42 PM
Varlu, i do not believe this will always give you the correct solution or a solution at all. The only way your method would work is if you have a linked list that is sorted and you know how it is sorted. This way you know which pointer to increment and when. Otherwise, you may actually "skip" over the merge point you are trying to find in your method. There are a lot of ways to correctly do this but none will be O(n). The easy way is just have two nested loops and traverse one list checking each point against every point in the other list. If you find a value that is in the other list, check to see if they share the same next element until you hit the end of the list. This is worst case O(n^2).
Go to the top of the page
+Quote Post
apicolet
no avatar
Newbie
*
Group: Members
Posts: 8
Joined: 22-November 07
From: Antibes, France
Member No.: 53,494



Post #3 post Nov 23 2007, 12:29 PM
Well, I think the answer depend on what are really these 'nodes'. If we consider them as objects holding the information on the next node - which forms the linkedlist - as well as their own data , then Varalu's solution would work. Indeed, if two nodes from the two linked lists are equal, it means they point to the same 'next node' object, which in turn will point to the same 'next node', etc. In this case, if the two linked list have a common node at some point, then the end of the two linked lists will be equal too. This, in turn, means that , if the two linked list have a common node, then this common node will be at the same position from the end in the two linked lists. Removing the extra nodes in the longest list and starting comparing from here thus work, in o(n).

For the case where elements of the list do not contain the reference to the 'next node' but only their value (such that you can have 1->2 in one list and 1->3 in the other), then we can think of a o(n) solution.
Suppose there is a way to traverse the list in the reverse order, beginning by the end and navigating to the previous (if the data structure does not enable it, then converting it into a data structurewhich enables it can be done in o(n) anyway). In this case, the algo would be :

0. mergingNode initialised to 'no merging node found (yet)'
1. currentNode from both lists = last node from both lists
2. compare both currentNodes
2.1 if they are different, return mergingNode
2.2 if they are equal :
2.2.1 mergingNode = currentNode
2.2.2 currentNodes from both list = previous element from both list (return mergingNode if one of the nodes does not exist - one list is finished)
2.2.3 go back to 2.
3. if we are here, the two lists are totally equal

This is an o(n) algorithm for that.



Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #4 post May 11 2009, 07:45 AM
stor liklist
Data Structures -- Linked List -- Point Of Merging

Task 2: Given a structure definition: struct employee{

int emp_id;

float emp_salary;

struct employee * llink;

struct employee * rlink;

 };

a. Create a doubly linked list by adding nodes using dynamic memory allocation. Structure of each node in the linked list is given above, make this linked list as circular and display the elements of the linked list.

b. Write a program/algorithm to a sort linked list created in 2 a.

-question by mimion

Go to the top of the page
+Quote Post
iGuest
no avatar
Hail Caesar!
*********************
Group: Members
Posts: 5,876
Joined: 21-September 07
Member No.: 50,369



Post #5 post Sep 25 2009, 11:47 PM

Please help me to find the solution of these questions :

1. How to separate one linked list that contains integer numbers into two linked list one of them contain even numbers and another contains odd numbers ?

2.How to merge two linked list into one linked list ?

-reply by Fulla
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No new   20 Ariel 15,491 11th August 2009 - 02:34 PM
Last post by: mahesh2k
No New Posts   2 (G)Kousiq Karim 41 20th March 2010 - 11:35 AM
Last post by: cons
No New Posts   2 threepach 2,633 19th February 2008 - 08:30 PM
Last post by: threepach
No New Posts   0 OpaQue 2,953 1st September 2004 - 06:16 PM
Last post by: OpaQue
No New Posts   4 err 9,196 25th January 2009 - 07:52 PM
Last post by: Antv912
No New Posts   2 varalu 1,166 17th December 2009 - 12:55 PM
Last post by: iG-om
No New Posts   0 DMA 2,873 27th October 2004 - 01:53 AM
Last post by: DMA
No New Posts   0 round 2,443 8th November 2004 - 07:41 PM
Last post by: round
No New Posts   5 RED NOVA 4,172 19th June 2005 - 08:52 PM
Last post by: M67
No New Posts 9 nonon 3,172 24th March 2005 - 04:55 PM
Last post by: nonon
No New Posts   1 MacFly 3,853 13th December 2004 - 10:47 PM
Last post by: serverph
No New Posts   0 Mike 2,623 28th December 2004 - 02:56 PM
Last post by: Mike
No New Posts   1 kvarnerexpress 2,689 24th January 2005 - 11:25 AM
Last post by: splehati
No New Posts   12 burgen 4,098 25th January 2005 - 03:59 PM
Last post by: xboxrulz
No New Posts 12 Damen 2,560 5th January 2008 - 01:32 AM
Last post by: Damen


 



RSS Open Discussion Time is now: 22nd March 2010 - 06:12 AM

Web Hosting Powered by ComputingHost.com.
Xisto.com : HONESTY ROCKS! truth rules.
Creative Commons License