Skip to content

Commit 9a4fec4

Browse files
committed
Merge branch 'release/1.0.1'
2 parents 17d23c3 + 5445fb7 commit 9a4fec4

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ to keep up with the language and learn how to use the Scala test library as well
1313
* Single number
1414
* Single number II
1515
* Single number III
16+
* Merge Two Sorted Lists
1617

1718
## Versions
19+
* 1.0.1 (03/30/2018) - Added "Merge Two Sorted Lists" exercise
1820

19-
* 1.0.0 - Initial version
21+
* 1.0.0 (03/30/2018) - Initial version
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.sharpsw.leetcode
2+
3+
object MergeSortedLinkedLists {
4+
def mergeTwoLists(l1: ListNode, l2: ListNode): ListNode = {
5+
var root:ListNode = null
6+
var lastNode:ListNode = null
7+
8+
var root1 = l1
9+
var root2 = l2
10+
11+
while(root1 != null || root2 != null) {
12+
var node: ListNode = null
13+
if(root1 != null && root2 != null) {
14+
if(root1._x < root2._x) {
15+
node = new ListNode(root1._x)
16+
root1 = root1.next
17+
} else {
18+
node = new ListNode(root2._x)
19+
root2 = root2.next
20+
}
21+
} else if(root1 == null) {
22+
node = new ListNode(root2._x)
23+
root2 = root2.next
24+
} else {
25+
node = new ListNode(root1._x)
26+
root1 = root1.next
27+
}
28+
29+
if(root == null) {
30+
root = node
31+
lastNode = root
32+
} else {
33+
lastNode.next = node
34+
lastNode = node
35+
}
36+
}
37+
root
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.sharpsw.leetcode
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
5+
class MergeSortedLinkedListsSpec extends FlatSpec with Matchers {
6+
"Given null lists to be merged" should "return null" in {
7+
val result = MergeSortedLinkedLists.mergeTwoLists(null, null)
8+
result should be (null)
9+
}
10+
11+
"Given 2 singled item lists to be merged" should "return [1, 1]" in {
12+
val list1 = new ListNode(1)
13+
val list2 = new ListNode(1)
14+
15+
val result = MergeSortedLinkedLists.mergeTwoLists(list1, list2)
16+
ListNodeUtils.length(result) shouldEqual 2
17+
ListNodeUtils.mkString(result) shouldEqual "1-1"
18+
}
19+
20+
"Given 2 singled item lists with values 1 and 2" should "return [1, 2]" in {
21+
val list1 = new ListNode(2)
22+
val list2 = new ListNode(1)
23+
24+
val result = MergeSortedLinkedLists.mergeTwoLists(list1, list2)
25+
ListNodeUtils.length(result) shouldEqual 2
26+
ListNodeUtils.mkString(result) shouldEqual "1-2"
27+
}
28+
29+
"Given 2 singled item lists with values [1, 2] and [3]" should "return [1, 2, 3]" in {
30+
val list1 = new ListNode(2)
31+
val list2 = new ListNode(1)
32+
val list22 = new ListNode(3)
33+
list2.next = list22
34+
35+
val result = MergeSortedLinkedLists.mergeTwoLists(list1, list2)
36+
ListNodeUtils.length(result) shouldEqual 3
37+
ListNodeUtils.mkString(result) shouldEqual "1-2-3"
38+
}
39+
}

0 commit comments

Comments
 (0)