Skip to content

Commit 84a7885

Browse files
committed
solve problem Reverse Linked List
1 parent e65d5c9 commit 84a7885

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ All solutions will be accepted!
103103
|492|[Construct The Rectangle](https://leetcode-cn.com/problems/construct-the-rectangle/description/)|[java/py/js]|(./algorithms/ConstructTheRectangle)|Easy|
104104
|447|[Number Of Boomerangs](https://leetcode-cn.com/problems/number-of-boomerangs/description/)|[java/py/js](./algorithms/NumberOfBoomerangs)|Easy|
105105
|824|[Goat Latin](https://leetcode-cn.com/problems/goat-latin/description/)|[java/py/js](./algorithms/GoatLatin)|Easy|
106+
|206|[Reverse Linked List](https://leetcode-cn.com/problems/reverse-linked-list/description/)|[java/py/js](./algorithms/ReverseLinkedList)|Easy|
106107

107108
# Database
108109
|#|Title|Solution|Difficulty|
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Reverse Linked List
2+
This problem is easy to solve, the loop solution is like below:
3+
```
4+
# Definition for singly-linked list.
5+
# class ListNode(object):
6+
# def __init__(self, x):
7+
# self.val = x
8+
# self.next = None
9+
10+
class Solution(object):
11+
def reverseList(self, head):
12+
"""
13+
:type head: ListNode
14+
:rtype: ListNode
15+
"""
16+
node = None
17+
while head:
18+
temp = ListNode(head.val)
19+
temp.next = node
20+
node = temp
21+
head = head.next
22+
return node
23+
```
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode reverseList(ListNode head) {
11+
return reverseListRecursively(null, head);
12+
}
13+
14+
public ListNode reverseListRecursively(ListNode pre, ListNode cur) {
15+
if (cur == null) return null;
16+
if (cur.next == null) {
17+
cur.next = pre;
18+
return cur;
19+
}
20+
21+
ListNode next = cur.next;
22+
cur.next = pre;
23+
return reverseListRecursively(cur, next);
24+
}
25+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function(head) {
13+
return reverseListRecursively(null, head)
14+
};
15+
16+
var reverseListRecursively = function (pre, cur) {
17+
if (cur === null) return null
18+
if (cur.next === null) {
19+
cur.next = pre
20+
return cur
21+
}
22+
23+
next = cur.next
24+
cur.next = pre
25+
return reverseListRecursively(cur, next)
26+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def reverseList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
return self.reverseListRecursively(None, head)
14+
15+
def reverseListRecursively(self, pre, cur):
16+
if cur == None:
17+
return None
18+
if cur.next == None:
19+
cur.next = pre
20+
return cur
21+
nxt = cur.next
22+
cur.next = pre
23+
return self.reverseListRecursively(cur, nxt)
24+

0 commit comments

Comments
 (0)