473,394 Members | 1,739 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,394 software developers and data experts.

Linked List

This was a question from GE interview when faced the GE interview..
Question : There is a one linked List with known starting and ending, you have to find out the middle of the linked list, you can traverse the whole linked list at once only....
Feb 19 '07 #1
6 1291
AdrianH
1,251 Expert 1GB
This was a question from GE interview when faced the GE interview..
Question : There is a one linked List with known starting and ending, you have to find out the middle of the linked list, you can traverse the whole linked list at once only....
That is easy, but unfortunately I don't think that we can give you the answer. It might be against policy. What do you other moderators think?


Adrian
Feb 19 '07 #2
DeMan
1,806 1GB
(Not that I'm a moderator....but) I can't see that there's any harm discussing the method........

The middle of a list is halfway from the start to the finish (obvious I know, but this is quite significant)

I will assume that traversing the list once only means "you can only use one loop", not "you can only use one pointer"

Step through the list:
Pseudocode:
Expand|Select|Wrap|Line Numbers
  1. counter == 0;
  2. this.element = first.element;
  3. mid.element=this.element;
  4. while(this.element != last.element)
  5. {
  6.    counter++;
  7.   if(counter % 2 == 0)
  8.   {
  9.     mid = mid.next();
  10.   }
  11.   this = this.next();
  12. }
  13.  
If there is 1 item, mid is correct, if there is an even number of items, thelater item is found, if there is an odd number, the middle is found (I think)
Feb 19 '07 #3
DeMan
1,806 1GB
And I probably shouldn't have used "this".......so call it "current" if you like
Feb 19 '07 #4
Hi,

There is another solution for the same...

Use 2 pointers p1 and p2, initially both should point to the head.
Advance p1... one node at a time
Advance p2... two node at a time.
When p2 reaches the end, p1 is in the middle.

Thanks
Saby Abraham
Feb 27 '07 #5
This was a question from GE interview when faced the GE interview..
Question : There is a one linked List with known starting and ending, you have to find out the middle of the linked list, you can traverse the whole linked list at once only....
Hi, i hope this works, because the starting and ending of the linked list is known one can traverse the list from either side, with a counter, thus same counter and the value at the pointer then break the loop. thus answer is out in 1/2 of the recurring loop with two pointers pointing either side of the linked list.
Feb 27 '07 #6
DeMan
1,806 1GB
Assuming it's a doubly linked list you could take the "let's meet in (near) the middle).
Knowing the start and end, however (even in a doubly linked list), doesn't help us to know how many elemetns are in between.....
Feb 27 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
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...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.