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 =...

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.

Force Button Address

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" * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> nodesSeen = new HashSet<>(); while (head != null) { if (nodesSeen.contains(head)) { return true; } else { nodesSeen.add(head); } head = head.next; } 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" * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { // Start from the chain header ListNode slow = head; ListNode fast = head; 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.

Advanced:

  • Can you solve this without extra space?

Force Button Address

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" * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { Set<ListNode> visited = new HashSet<ListNode>(); ListNode node = head; while (node != null) { if (visited.contains(node)) { return node; } visited.add(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" * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; 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){ fast = head; // 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; } }

13 January 2020, 18:51 | Views: 8554

Add new comment

For adding a comment, please log in
or create account

0 comments