|
INTERVIEW QUESTIONS
PROGRAMMING LANGUAGES
C++
DETAILS
Question: ell how to check whether a linked list is circular.
Answer:
Create two pointers, each set to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print ("circular
"); } }
OK, why does this work? If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.
|
|
|
Category |
C++ Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7170 users |
Added on |
7/16/2015 |
Views |
65608 |
Rate it! |
|
|
Question:
ell how to check whether a linked list is circular.
Answer:
Create two pointers, each set to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print ("circular
"); } }
OK, why does this work? If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Write a function that swaps the values of two integers, using int* as the argument type.
|
View Answer
|
|
Write a short code using C++ to print out all odd number from 1 to 100 using a for loop
|
View Answer
|
|
Write a program that ask for user input from 5 to 9 then calculate the average
|
View Answer
|
|
In the derived class, which data member of the base class are visible?
|
View Answer
|
|
STL Containers - What are the types of STL containers?
|
View Answer
|
|
How do you traverse a Btree in Backward in-order?
|
View Answer
|
|
What is pure virtual function?
|
View Answer
|
|
How can you tell what shell you are running on UNIX system?
|
View Answer
|
|
How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
|
View Answer
|
|
What is polymorphism?
|
View Answer
|
|
How do you write a function that can reverse a linked-list?
|
View Answer
|
|
What is a container class? What are the types of container classes?
|
View Answer
|
|
What is an orthogonal base class?
|
View Answer
|
|
What is a node class?
|
View Answer
|
|
What are proxy objects?
|
View Answer
|
|
What are the conditions that have to be met for a condition to be an invariant of the class?
|
View Answer
|
|
Define precondition and post-condition to a member function.
|
View Answer
|
|
What do you mean by Stack unwinding?
|
View Answer
|
|
What is class invariant?
|
View Answer
|
|
What is a Null object?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All C++ Interview Questions & Answers - Exam Mode /
Learning Mode
|