title: List of Lists: how to add a trailing 0 for each List except the last one?
body: <p>List of Lists: how to add a trailing 0 for each List except the last one?</p>
  
  <p>I'm learning Scala. I have a List of Lists, like below:</p>
  
  <pre><code>List(List(1,2,3), List(15, 17, 21), List(28, 5, 7))
  </code></pre>
  
  <p>My aim is to add a trailing 0 for each List except the last one, like below:</p>
  
  <pre><code>List(List(1,2,3, 0), List(15, 17, 21, 0), List(28, 5, 7))
  </code></pre>
  
  <p>My solution is like below:</p>
  
  <pre><code>def addZero(lines: List[List[Int]]): List[Int] = {
       def helper(nums: List[Int]): List[Int] = nums match {
         case Nil =&gt; 0 :: Nil
         case hd :: tl =&gt; hd :: helper(tl)
       }
  
       lines match {
         case Nil =&gt; Nil
         case hd :: Nil =&gt; hd
         case hd :: tl =&gt; helper(hd) ++ addZero(tl)
       }
  }
  </code></pre>
  
  <p>But I'm not sure if there is more elegant way. I tried <code>flatMap</code> and <code>foldLeft</code> but they add 0 to every List
  including the last one.</p>