ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Solidity: Detect Cycle in Linked List

Solidity

Goal -- WPM

Ready
Exercise Algorithm Area
1pragma solidity ^0.8.0;
2
3contract LinkedListCycleDetection {
4
5struct Node {
6uint256 value;
7Node next;
8}
9
10// Helper function to create a new node
11function createNode(uint256 _value, Node _next) internal pure returns (Node memory) {
12return Node({value: _value, next: _next});
13}
14
15// Function to detect a cycle in a linked list
16function hasCycle(Node head) public pure returns (bool) {
17if (head == Node(0)) { // Check for empty list
18return false;
19}
20
21Node tortoise = head;
22Node hare = head;
23
24// Move hare two steps and tortoise one step at a time
25// The loop continues as long as hare and hare.next are not null
26while (hare != Node(0) && hare.next != Node(0)) {
27tortoise = tortoise.next;
28hare = hare.next.next;
29
30// If tortoise and hare meet, there is a cycle
31if (tortoise == hare) {
32return true;
33}
34}
35
36// If hare reaches the end of the list, there is no cycle
37return false;
38}
39
40// Example usage (for testing purposes, not part of the core algorithm)
41function testCycle() public pure returns (bool) {
42// Create a list with a cycle: 1 -> 2 -> 3 -> 2
43Node node3 = createNode(3, Node(0)); // Placeholder for next, will be overwritten
44Node node2 = createNode(2, node3);
45Node node1 = createNode(1, node2);
46node3.next = node2; // Create the cycle
47
48return hasCycle(node1);
49}
50
51function testNoCycle() public pure returns (bool) {
52// Create a list without a cycle: 1 -> 2 -> 3
53Node node3 = createNode(3, Node(0));
54Node node2 = createNode(2, node3);
55Node node1 = createNode(1, node2);
56
57return hasCycle(node1);
58}
59}
Algorithm description viewbox

Solidity: Detect Cycle in Linked List

Algorithm description:

This Solidity contract implements Floyd's cycle-finding algorithm, also known as the tortoise and hare algorithm, to detect cycles in a singly linked list. It defines a `Node` struct and a `hasCycle` function that uses two pointers moving at different speeds. This is crucial for smart contracts managing data structures that might inadvertently form cycles, leading to infinite loops or denial-of-service vulnerabilities.

Algorithm explanation:

The `hasCycle` function takes the `head` of a singly linked list as input. It first handles the edge case of an empty list by checking if `head` is a null `Node` (represented as `Node(0)` in Solidity for structs). If the list is empty, it returns `false`. Two pointers, `tortoise` and `hare`, are initialized to the `head`. The `hare` pointer moves two steps forward in each iteration, while the `tortoise` pointer moves one step. This is achieved by updating `tortoise = tortoise.next` and `hare = hare.next.next`. The loop continues as long as `hare` and `hare.next` are not null, ensuring that `hare.next.next` can be safely accessed. If at any point `tortoise` and `hare` point to the same `Node`, it signifies that a cycle has been detected, and the function returns `true`. If the `hare` pointer reaches the end of the list (becomes null) without meeting the `tortoise`, it means there is no cycle, and the function returns `false`. The time complexity is O(n), where n is the number of nodes in the list, because in the worst case, the hare traverses the list twice. The space complexity is O(1) as only two pointers are used, regardless of the list's size.

Pseudocode:

struct Node { value, next }

function hasCycle(head):
  if head is null:
    return false

  tortoise = head
  hare = head

  while hare is not null and hare.next is not null:
    tortoise = tortoise.next
    hare = hare.next.next

    if tortoise == hare:
      return true

  return false