# [small Y learning algorithm] ⚡ Daily LeetCode punch in ⚡ Table 38. Circular linked list

Posted by 1981tarun on Wed, 22 Sep 2021 04:13:01 +0200 #### 📢 preface

 🚀 Algorithm problem 🚀
• 🌲 Punching out an algorithm problem every day is not only a learning process, but also a sharing process 😜
• 🌲 Tip: the problem-solving programming languages in this column are C# and Java
• 🌲 To maintain a state of learning every day, let's work together to become the great God of algorithm 🧐!
• 🌲 Today is the 38th day of punching out the force deduction algorithm 🎈!
 🚀 Algorithm problem 🚀

## 🌲 Example of original question: circular linked list

If there is a node in the linked list that can be reached again by continuously tracking the next pointer, there is a ring in the linked list. In order to represent the rings in a given list, we use the integer pos to represent the position where the tail of the list is connected to the list (the index starts from 0). If pos is - 1, there is no ring in the linked list.

Note: pos is not passed as a parameter, but only to identify the actual situation of the linked list.

Returns true if there are links in the linked list. Otherwise, false is returned.

Example 1: ```Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: there is a ring in the linked list, and its tail is connected to the second node.
```

Example 2: ```Input: head = [1,2], pos = 0
Output: true
Explanation: there is a ring in the linked list, and its tail is connected to the first node.
```

Example 3: ```Input: head = , pos = -1
Output: false
```

Tips:

• The number range of nodes in the linked list is [0, 104]
• -105 <= Node.val <= 105
• pos is - 1 or a valid index in the linked list

### 🌻 C# method: double pointer

Train of thought analysis

The fast and slow pointer starts from the node at the same time. The fast pointer takes two steps at a time and the slow pointer takes one step at a time. If there is a ring, the fast pointer will catch up with the slow pointer sooner or later.

code:

```public class Solution {
{

while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
{
return true;
}
}

return false;
}
}
```

results of enforcement

```adopt
Execution time: 92 ms，At all C# Defeated 74.50% of users in submission
Memory consumption: 26.6 MB，At all C# Beat 75.17% of users in submission
```

### 🌻 Java method: hash table

Train of thought analysis

The easiest way to think of is to traverse all nodes. Each time you traverse a node, judge whether the node has been accessed before.

Specifically, we can use a hash table to store all the nodes that have been accessed.
Every time we reach a node, if the node already exists in the hash table, it means that the linked list is a ring linked list, otherwise the node will be added to the hash table. Repeat this process until we traverse the entire linked list.

code:

```class Solution {
public boolean isPalindrome(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
return sgood.toString().equals(sgood_rev.toString());
}
}
```

results of enforcement

```adopt
Execution time: 4 ms，At all Java  Defeated 20 in submission.23%User
Memory consumption: 39.1 MB，At all Java Defeated 83 in submission.75%User
```

Complexity analysis

```Time complexity: O( N ),among N Is the number of nodes in the linked list
Space complexity: O( N  )，among N Is the number of nodes in the linked list
```

## 💬 summary

• Today is the 38th day of punch in of force deduction algorithm!
• This paper uses C# and Java programming languages to solve problems
• Some methods are also written by Likou God, and they are also shared while learning. Thanks again to the algorithm bosses
• That's the end of today's algorithm sharing. See you tomorrow! 