Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Circular singly linked list

Nice to solve before

Instructions

Given a linked list, return true if the list is circular, false if it is not.

Challenge | Solution

Examples

Example 1

val l = new SinglyLinkedList ()
val a = new Node ('a')
val b = new Node ('b')
val c = new Node ('c')
l.head = a
a.next = b
b.next = c
c.next = b
circular(l) // true

Example 2

val l = new List ()
val a = new Node ('a')
circular(l) // false

Hints

Hint 1 We should use more then one variable to store values that are retrieved during iteration (two pointer solution)
Hint 2 Name of these variables should be `slow` and `fast`
Hint 3 Assign next node to `slow` variable in every iteration
Hint 4 Assign next node of next node to `fast` variable in every iteration