diff --git a/problems/0024.两两交换链表中的节点.md b/problems/0024.两两交换链表中的节点.md
index ce75e0d7..2289c229 100644
--- a/problems/0024.两两交换链表中的节点.md
+++ b/problems/0024.两两交换链表中的节点.md
@@ -311,7 +311,29 @@ func swapPairs(_ head: ListNode?) -> ListNode? {
return dummyHead.next
}
```
-
+Scala:
+```scala
+// 虚拟头节点
+object Solution {
+ def swapPairs(head: ListNode): ListNode = {
+ var dummy = new ListNode(0, head) // 虚拟头节点
+ var pre = dummy
+ var cur = head
+ // 当pre的下一个和下下个都不为空,才进行两两转换
+ while (pre.next != null && pre.next.next != null) {
+ var tmp: ListNode = cur.next.next // 缓存下一次要进行转换的第一个节点
+ pre.next = cur.next // 步骤一
+ cur.next.next = cur // 步骤二
+ cur.next = tmp // 步骤三
+ // 下面是准备下一轮的交换
+ pre = cur
+ cur = tmp
+ }
+ // 最终返回dummy虚拟头节点的下一个,return可以省略
+ dummy.next
+ }
+}
+```
-----------------------
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md
index 941928ba..25b16907 100644
--- a/problems/0206.翻转链表.md
+++ b/problems/0206.翻转链表.md
@@ -496,6 +496,40 @@ struct ListNode* reverseList(struct ListNode* head){
return reverse(NULL, head);
}
```
+Scala:
+双指针法:
+```scala
+object Solution {
+ def reverseList(head: ListNode): ListNode = {
+ var pre: ListNode = null
+ var cur = head
+ while (cur != null) {
+ var tmp = cur.next
+ cur.next = pre
+ pre = cur
+ cur = tmp
+ }
+ pre
+ }
+}
+```
+递归法:
+```scala
+object Solution {
+ def reverseList(head: ListNode): ListNode = {
+ reverse(null, head)
+ }
+
+ def reverse(pre: ListNode, cur: ListNode): ListNode = {
+ if (cur == null) {
+ return pre // 如果当前cur为空,则返回pre
+ }
+ val tmp: ListNode = cur.next
+ cur.next = pre
+ reverse(cur, tmp) // 此时cur成为前一个节点,tmp是当前节点
+ }
+}
+```
-----------------------
diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md
index 37ce15ad..dcdb53f4 100644
--- a/problems/0707.设计链表.md
+++ b/problems/0707.设计链表.md
@@ -1154,7 +1154,75 @@ class MyLinkedList {
}
```
+Scala:
+```scala
+class ListNode(_x: Int = 0, _next: ListNode = null) {
+ var next: ListNode = _next
+ var x: Int = _x
+}
+class MyLinkedList() {
+
+ var size = 0 // 链表尺寸
+ var dummy: ListNode = new ListNode(0) // 虚拟头节点
+
+ // 获取第index个节点的值
+ def get(index: Int): Int = {
+ if (index < 0 || index >= size) {
+ return -1;
+ }
+ var cur = dummy
+ for (i <- 0 to index) {
+ cur = cur.next
+ }
+ cur.x // 返回cur的值
+ }
+
+ // 在链表最前面插入一个节点
+ def addAtHead(`val`: Int) {
+ addAtIndex(0, `val`)
+ }
+
+ // 在链表最后面插入一个节点
+ def addAtTail(`val`: Int) {
+ addAtIndex(size, `val`)
+ }
+
+ // 在第index个节点之前插入一个新节点
+ // 如果index等于链表长度,则说明新插入的节点是尾巴
+ // 如果index等于0,则说明新插入的节点是头
+ // 如果index>链表长度,则说明为空
+ def addAtIndex(index: Int, `val`: Int) {
+ if (index > size) {
+ return
+ }
+ var loc = index // 因为参数index是val不可变类型,所以需要赋值给一个可变类型
+ if (index < 0) {
+ loc = 0
+ }
+ size += 1 //链表尺寸+1
+ var pre = dummy
+ for (i <- 0 until loc) {
+ pre = pre.next
+ }
+ val node: ListNode = new ListNode(`val`, pre.next)
+ pre.next = node
+ }
+ // 删除第index个节点
+ def deleteAtIndex(index: Int) {
+ if (index < 0 || index >= size) {
+ return
+ }
+ size -= 1
+ var pre = dummy
+ for (i <- 0 until index) {
+ pre = pre.next
+ }
+ pre.next = pre.next.next
+ }
+
+}
+```
-----------------------