Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 9 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
May 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
添加 0707.设计链表.md Scala版本
  • Loading branch information
wzqwtt committed May 11, 2022
commit 898016b8a98aa7eeb2445681e66f0302a79c28ae
68 changes: 68 additions & 0 deletions problems/0707.设计链表.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
```


-----------------------
Expand Down

AltStyle によって変換されたページ (->オリジナル) /