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....
6 1291
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
(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: -
counter == 0;
-
this.element = first.element;
-
mid.element=this.element;
-
while(this.element != last.element)
-
{
-
counter++;
-
if(counter % 2 == 0)
-
{
-
mid = mid.next();
-
}
-
this = this.next();
-
}
-
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)
And I probably shouldn't have used "this".......so call it "current" if you like
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
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.
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.....
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |