Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Question Protected by Travis J
In fact, the phrase "Interview question" was not in the original quest, it was added later, by someone else than the author. And this phrase narrows the scope of a larger quest, interesting in itself. Let's get rid of this uninteresting phrase !
Source Link

Interview question.

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?

Here's a picture of what a list with a loop looks like:

alt text

Interview question.

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?

Here's a picture of what a list with a loop looks like:

alt text

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?

Here's a picture of what a list with a loop looks like:

alt text

Restoration of the author's quest. The extra requirement changes the meaning of the quest, and I prefer the original quest. The edit guidelines say "*always* respect the original author". "Interview question" moved, the word "question" is refused in the title anyway.
Source Link

Interview question: How to detect a loop in a linked list?

Interview question.

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time? (should be O(n) time)

Here's a picture of what a list with a loop looks like:

alt text

Interview question: How to detect a loop in a linked list?

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time? (should be O(n) time)

Here's a picture of what a list with a loop looks like:

alt text

How to detect a loop in a linked list?

Interview question.

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?

Here's a picture of what a list with a loop looks like:

alt text

added 22 characters in body
Source Link
nonopolarity
  • 151.8k
  • 142
  • 495
  • 783

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time? (should be O(n) time)

Here's a picture of what a list with a loop looks like:

alt text

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time?

Here's a picture of what a list with a loop looks like:

alt text

Say you have a linked list structure in Java. It's made up of Nodes:

class Node {
 Node next;
 // some user data
}

and each Node points to the next node, except for the last Node, which has null for next. Say there is a possibility that the list can contain a loop - i.e. the final Node, instead of having a null, has a reference to one of the nodes in the list which came before it.

What's the best way of writing

boolean hasLoop(Node first)

which would return true if the given Node is the first of a list with a loop, and false otherwise? How could you write so that it takes a constant amount of space and a reasonable amount of time? (should be O(n) time)

Here's a picture of what a list with a loop looks like:

alt text

deleted 68 characters in body; edited tags; edited title
Source Link
codaddict
  • 456.6k
  • 83
  • 500
  • 537
Loading
edited title
Link
codaddict
  • 456.6k
  • 83
  • 500
  • 537
Loading
edited tags
Link
Foredecker
  • 7.5k
  • 4
  • 32
  • 30
Loading
added 190 characters in body
Source Link
jjujuma
  • 22.9k
  • 12
  • 46
  • 48
Loading
Source Link
jjujuma
  • 22.9k
  • 12
  • 46
  • 48
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /