eolymp
bolt
Try our new interface for solving problems
Problems

LinkedList Remove Cycle

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
{
public:
  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)

Example

prb10042_1.gif

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

prb10748.gif

Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev