Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. import org.scalatest.{Matchers, WordSpec}
  2.  
  3. import scala.annotation.tailrec
  4.  
  5. /**
  6. * You have two numbers represented by a linked list, where each node contains a single digit.
  7. * The digits are stored in reverse order, such that the 1's digit is at the head of the list.
  8. * Write a function that adds the two numbers and returns the sum as a linked list.
  9. *
  10. * Assumptions: both list has equal number of nodes
  11. *
  12. * for example (7-> 1 -> 6->Nil) + (5-> 9 -> 2-> Nil).That is,617 + 295 = 912
  13. * output will be : 9->1->2-Nil return new linklist
  14. * time complexity O(n+n)
  15. * space complexity O(n) //new linklist
  16. *
  17. * Created by Arun Sethia on 7/22/18.
  18. */
  19. trait Linklist {
  20.  
  21. /**
  22. * abstract Node
  23. */
  24. abstract class AbstractNode {
  25. def data: Int
  26.  
  27. def next: AbstractNode
  28. }
  29.  
  30. /**
  31. * Nil Node to indicate end of link list
  32. */
  33. object NilNode extends AbstractNode {
  34. def data = 0
  35.  
  36. override def toString() = s"Nil"
  37.  
  38. def next = throw new UnsupportedOperationException("tail of empty list")
  39. }
  40.  
  41. /**
  42. * Node class
  43. *
  44. * @param data
  45. * @param next
  46. */
  47. case class Node(data: Int, next: AbstractNode) extends AbstractNode {
  48. override def toString() = s"data: ${data} next: ${next}"
  49. }
  50.  
  51. /**
  52. * add new node in the beginning
  53. *
  54. * @param prevNode
  55. */
  56. implicit class NodeAddItem(prevNode: AbstractNode) {
  57.  
  58. def prependItem(v: Int): AbstractNode = Node(v, prevNode)
  59.  
  60. /**
  61. * prepand node2 with prevNode
  62. *
  63. * @param node2
  64. * @return
  65. */
  66. def prepand(node2: AbstractNode): AbstractNode = {
  67. if (node2.next != NilNode) prepand(node2.next).prepand(Node(node2.data, NilNode))
  68. else Node(node2.data, prevNode)
  69. }
  70. }
  71.  
  72.  
  73. /**
  74. * sum two linklist nodes and create new linklist
  75. * for example (7-> 1 -> 6->Nil) + (5-> 9 -> 2-> Nil).That is,617 + 295 = 912
  76. * output will be : 9->1->2-Nil
  77. *
  78. * @param node1
  79. * @param node2
  80. * @return
  81. */
  82. def addSumList(node1: AbstractNode, node2: AbstractNode): AbstractNode = {
  83. @tailrec
  84. def sumList(node1: AbstractNode, node2: AbstractNode, finalNode: AbstractNode, carryOver: Int): AbstractNode = {
  85. if (node1.next != NilNode && node2.next != NilNode) {
  86. val (data, newcarryOver) = getSumReminder(node1.data, node2.data, carryOver)
  87. val node = finalNode.prepand(Node(data, NilNode))
  88. sumList(node1.next, node2.next, node, newcarryOver)
  89. } else {
  90. val (data, newcarryOver) = getSumReminder(node1.data, node2.data, carryOver)
  91. val tmpNode = finalNode.prependItem(data)
  92. //add carryover for final result
  93. if (newcarryOver > 0)
  94. tmpNode.prependItem(newcarryOver)
  95. else tmpNode //don't add carryover for final result
  96. }
  97. }
  98. sumList(node1, node2, NilNode, 0)
  99. }
  100.  
  101. /**
  102. * get sum all number and get sum and carryon for given numbers
  103. *
  104. * @return
  105. */
  106. private def getSumReminder(d: Int*): (Int, Int) = {
  107. val sum = d.sum
  108. val data = (sum % 10)
  109. val nextCarryon = sum / 10
  110. (data, nextCarryon)
  111. }
  112.  
  113. }
  114.  
  115. /**
  116. * test cases for sumList
  117. */
  118. class LinkSumListSpec extends WordSpec
  119. with Matchers
  120. with Linklist {
  121.  
  122. "(7-> 1 -> 6->Nil) + (5-> 9 -> 2-> Nil)" should {
  123. "return 9->1->2-Nil" in {
  124. val node1 = Node(7, Node(1, Node(6, NilNode)))
  125. val node2 = Node(5, Node(9, Node(2, NilNode)))
  126. val result = Node(9, Node(1, Node(2, NilNode)))
  127. assert(addSumList(node1, node2) == result)
  128. }
  129. }
  130.  
  131. "(9-> 8 -> 7-> 6-> Nil) + (9-> 8 -> 7-> 6-> Nil)" should {
  132. "return 1->3->5->7->8->Nil" in {
  133. val node1 = Node(9, Node(8, Node(7, Node(6, NilNode))))
  134. val node2 = Node(9, Node(8, Node(7, Node(6, NilNode))))
  135. val result = Node(1,Node(3, Node(5, Node(7, Node(8, NilNode)))))
  136. assert(addSumList(node1, node2) == result)
  137. }
  138. }
  139.  
  140. }
Add Comment
Please, Sign In to add comment