w3resource

Scala Programming: Check a given list is a palindrome or not

Scala Programming List Exercise-17 with Solution

Write a Scala program to check a given list is a palindrome or not.

Sample Solution:

Scala Code:

object Scala_List {
  
  def is_Palindrome[A](list_nums: List[A]):Boolean = {
    list_nums == list_nums.reverse
}
     
   def main(args: Array[String]): Unit = {
         println("Result: " + is_Palindrome(List(1,2,3,4,3,2,1)));
         println("Result: " + is_Palindrome(List(1,2,3)));
       }
}

Sample Output:

Result: true
Result: false

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to reverse a given list.
Next: Write a Scala program to flatten a given List of Lists, nested list structure.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.