Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit f9aeb3d

Browse files
committed
slt200
1 parent cfd8809 commit f9aeb3d

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

countByAuthors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#### Order by Join time
22
#### 1.Scala
3-
dreamylost:=> 113
3+
dreamylost:=> 114
44

55
sweeneycai:=> 39
66

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Licensed under Apache-2.0 (C) All Contributors */
2+
package io.github.dreamylost
3+
4+
/**
5+
* 206. 反转链表
6+
*
7+
* @author 梦境迷离
8+
* @since 2021/12/1
9+
* @version 1.0
10+
*/
11+
object Leetcode_200 {
12+
13+
def reverseList(head: ListNode): ListNode = {
14+
if (head == null || head.next == null) return head
15+
val newHead = reverseList(head.next)
16+
head.next.next = head
17+
head.next = null
18+
newHead
19+
}
20+
21+
/**
22+
* 500 ms,18.00%
23+
* 52.6 MB,94.00%
24+
*
25+
* @param head
26+
* @return
27+
*/
28+
def reverseList2(head: ListNode): ListNode = {
29+
var newHead: ListNode = null
30+
var curHead = head
31+
while (curHead != null) {
32+
val tail = curHead.next //保存其余节点,防止丢尾部
33+
curHead.next = newHead //断开链,链接新的头节点
34+
newHead = curHead // 换头节点,头左移动
35+
curHead = tail // 移动旧链
36+
}
37+
newHead
38+
}
39+
40+
}

0 commit comments

Comments
 (0)