You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of checking if (head == NULL) and then else if (head->next == head), you could directly combine these into one check for clarity:
if (head == NULL || head->next == head)
return true;
Here's a slightly refined version
#include
using namespace std;
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
class Solution {
public:
// Function to check if the linked list has a loop.
bool detectLoop(Node *head) {
if (head == NULL || head->next == head)
return true;
Instead of checking if (head == NULL) and then else if (head->next == head), you could directly combine these into one check for clarity:
if (head == NULL || head->next == head)
return true;
Here's a slightly refined version
#include
using namespace std;
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
class Solution {
public:
// Function to check if the linked list has a loop.
bool detectLoop(Node *head) {
if (head == NULL || head->next == head)
return true;
};
The text was updated successfully, but these errors were encountered: