|
|
|
|
![]() ![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Post
#1
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) |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED]
Posts: 936 Joined: 14-April 05 From: West Chester, PA Member No.: 5,636 |
Post
#2
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).
|
![]() Group: Members
Posts: 8 Joined: 22-November 07 From: Antibes, France Member No.: 53,494 |
Post
#3
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. |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 5,876 Joined: 21-September 07 Member No.: 50,369 |
Post
#4
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 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members
Posts: 5,876 Joined: 21-September 07 Member No.: 50,369 |
Post
#5
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 |
![]() ![]() |
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
20 | Ariel | 14,774 | 11th August 2009 - 02:34 PM Last post by: mahesh2k |
|||
![]() |
2 | threepach | 2,167 | 19th February 2008 - 08:30 PM Last post by: threepach |
|||
![]() |
0 | OpaQue | 2,851 | 1st September 2004 - 06:16 PM Last post by: OpaQue |
|||
![]() |
4 | err | 8,919 | 25th January 2009 - 07:52 PM Last post by: Antv912 |
|||
![]() |
1 | varalu | 950 | 12th December 2007 - 04:00 PM Last post by: laexter |
|||
![]() |
0 | DMA | 2,749 | 27th October 2004 - 01:53 AM Last post by: DMA |
|||
![]() |
0 | round | 2,373 | 8th November 2004 - 07:41 PM Last post by: round |
|||
![]() |
5 | RED NOVA | 4,083 | 19th June 2005 - 08:52 PM Last post by: M67 |
|||
![]() |
9 | nonon | 3,106 | 24th March 2005 - 04:55 PM Last post by: nonon |
|||
![]() |
1 | MacFly | 3,694 | 13th December 2004 - 10:47 PM Last post by: serverph |
|||
![]() |
0 | Mike | 2,523 | 28th December 2004 - 02:56 PM Last post by: Mike |
|||
![]() |
1 | kvarnerexpress | 2,603 | 24th January 2005 - 11:25 AM Last post by: splehati |
|||
![]() |
12 | burgen | 3,955 | 25th January 2005 - 03:59 PM Last post by: xboxrulz |
|||
![]() |
12 | Damen | 2,251 | 5th January 2008 - 01:32 AM Last post by: Damen |
|||
![]() |
7 | ashmaninc | 3,606 | 7th February 2005 - 02:36 PM Last post by: OpaQue |
|||
|
Open Discussion | Time is now: 22nd November 2009 - 10:21 AM |
Web Hosting Powered by ComputingHost.com.