# Algorithms Ring Chain List II

00141 Ring Chain List

### Title Description

Given a list of chains, determine if there are rings in the list.

Instance 1:

```Input: head = [3,2,0,-4], pos = 1
Output:true
Interpretation: There is a ring in the chain table whose tail is connected to the second node.
```

Example 2:

```Input: head = [1,2], pos = 0
Output:true
Interpretation: There is a ring in the chain table whose tail is connected to the first node.
```

Example 3:

```Input: head = [1], pos = -1
Output: false
Interpretation: There are no rings in the list.
```

### shopping spree

#### Hashtable

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Determine whether a chain table is a ring list by checking whether a node has been visited before.A common method is to use a hash table.

``````/**
* WeChat Public Number "Ape Brush Title"
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
Set<ListNode> nodesSeen = new HashSet<>();
return true;
} else {
}
}
return false;
}
}
``````

#### Double Pointer

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Defines two different speeds of the fast and slow pointers to traverse the chain table, one step at a time for the slow pointer and two steps for the fast pointer.If there are no rings in the list, the final fast pointer will reach the end first and we can return false

``````/**
* WeChat Public Number "Ape Brush Title"
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
// Start from the chain header
while(fast != null && fast.next != null){
// Fast and Slow Pointer Walking
fast = fast.next.next;
slow = slow.next;
// Meeting means ringing
if(slow == fast){
return true;
}
}
return false;
}
}
``````

00142 Ring Chain Table II

### Title Description

Given a list of chains, returns the first node where the list of chains begins to ring.Returns null if the list is not looped.

To represent rings in a given chain table, we use the integer pos to represent where the end of the chain is connected to the chain table (the index starts at 0).If pos is -1, there are no rings in the list.

Description: Modifying a given list of chains is not allowed.

Example 1:

```Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
```

Interpretation: There is a ring in the chain table whose tail is connected to the second node.

Example 2:

```Input: head = [1,2], pos = 0
Output: tail connects to node index 0
```

Interpretation: There is a ring in the chain table whose tail is connected to the first node.

Example 3:

```Input: head = [1], pos = -1
Output: no cycle
```

Interpretation: There are no rings in the list.

• Can you solve this without extra space?

### shopping spree

#### Hashtable

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

If we save visited nodes with a Set, we can iterate through the list and return to the first node where a duplicate occurs

``````/**
* WeChat Public Number "Ape Brush Title"
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
Set<ListNode> visited = new HashSet<ListNode>();

while (node != null) {
if (visited.contains(node)) {
return node;
}
node = node.next;
}

return null;
}
}
``````

#### Double Pointer

This question is provided by WeChat Public Ape Brush Title. Errors are welcome to correct.

Illustrate fast and slow pointer judgment ring-ring entry algorithm.

``````/**
* WeChat Public Number "Ape Brush Title"
* class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {
while(fast != null && fast.next != null){
fast = fast.next.next;  // Fast Pointer Move 2 Bits
slow = slow.next;       // Slow Pointer Move 1 Bit
// The processing of fast and slow pointers can only tell if there are rings, not where they appear (encounters indicate rings)
if(fast == slow){
// Reset the Quick Pointer, then move one bit each, and the next place you meet is at the Entrance of the Ring
while(slow != fast){
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
}
``````

Tags: Programming

Posted on Mon, 13 Jan 2020 18:51:57 -0500 by Henks