Linked List practice

LinkedList Remove Cycle

Given a linked list. Remove a cycle in it. If there is no cycle, do nothing.

Definition of a single linked list:

// Java
class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
// C++
class ListNode
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}
// C
struct ListNode
  int val;
  struct ListNode *next;

Implement a function removeCycle that assigns null to the next pointer of the tail and returns pointer to the head of the list.

// Java
ListNode removeCycle(ListNode head)
// C, C++
ListNode* removeCycle(ListNode *head)



Function removeCycle removes the pointer from the tail (sets the tail pointer next to null) and returns head:


Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev