Skip to content

Commit f239622

Browse files
committed
206-reverse-linked-list.md Added C++, Go, Ruby solutions.
1 parent dbd8fb8 commit f239622

1 file changed

Lines changed: 125 additions & 5 deletions

File tree

solutions/1-1000/206-reverse-linked-list.md

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ Given the `head` of a singly linked list, reverse the list, and return _the reve
99

1010
### [Example 1]
1111
![](../../images/examples/206_1.jpg)
12+
1213
**Input**: `head = [1,2,3,4,5]`
1314

1415
**Output**: `[5,4,3,2,1]`
1516

1617
### [Example 2]
1718
![](../../images/examples/206_2.jpg)
19+
1820
**Input**: `[1,2]`
1921

2022
**Output**: `[2,1]`
@@ -77,6 +79,15 @@ while (current != null) {
7779

7880
## Java
7981
```java
82+
/**
83+
* public class ListNode {
84+
* int val;
85+
* ListNode next;
86+
* ListNode() {}
87+
* ListNode(int val) { this.val = val; }
88+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
89+
* }
90+
*/
8091
class Solution {
8192
public ListNode reverseList(ListNode head) {
8293
ListNode previous = null;
@@ -117,27 +128,136 @@ class Solution:
117128

118129
## C++
119130
```cpp
120-
// Welcome to create a PR to complete the code of this language, thanks!
131+
/**
132+
* struct ListNode {
133+
* int val;
134+
* ListNode *next;
135+
* ListNode() : val(0), next(nullptr) {}
136+
* ListNode(int x) : val(x), next(nullptr) {}
137+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
138+
* };
139+
*/
140+
class Solution {
141+
public:
142+
ListNode* reverseList(ListNode* head) {
143+
ListNode* previous = nullptr;
144+
ListNode* current = head;
145+
146+
while (current) {
147+
auto temp_next = current->next;
148+
current->next = previous;
149+
previous = current;
150+
current = temp_next;
151+
}
152+
153+
return previous;
154+
}
155+
};
121156
```
122157

123158
## JavaScript
124159
```javascript
125-
// Welcome to create a PR to complete the code of this language, thanks!
160+
/**
161+
* function ListNode(val, next) {
162+
* this.val = (val===undefined ? 0 : val)
163+
* this.next = (next===undefined ? null : next)
164+
* }
165+
*/
166+
var reverseList = function (head) {
167+
let previous = null
168+
let current = head
169+
170+
while (current != null) {
171+
const tempNext = current.next
172+
current.next = previous
173+
previous = current
174+
current = tempNext
175+
}
176+
177+
return previous
178+
};
126179
```
127180

128181
## C#
129182
```c#
130-
// Welcome to create a PR to complete the code of this language, thanks!
183+
/**
184+
* public class ListNode {
185+
* public int val;
186+
* public ListNode next;
187+
* public ListNode(int val=0, ListNode next=null) {
188+
* this.val = val;
189+
* this.next = next;
190+
* }
191+
* }
192+
*/
193+
public class Solution
194+
{
195+
public ListNode ReverseList(ListNode head)
196+
{
197+
ListNode previous = null;
198+
ListNode current = head;
199+
200+
while (current != null)
201+
{
202+
var tempNext = current.next;
203+
current.next = previous;
204+
previous = current;
205+
current = tempNext;
206+
}
207+
208+
return previous;
209+
}
210+
}
131211
```
132212

133213
## Go
134214
```go
135-
// Welcome to create a PR to complete the code of this language, thanks!
215+
/**
216+
* Definition for singly-linked list.
217+
* type ListNode struct {
218+
* Val int
219+
* Next *ListNode
220+
* }
221+
*/
222+
func reverseList(head *ListNode) *ListNode {
223+
var previous *ListNode
224+
current := head
225+
226+
for current != nil {
227+
tempNext := current.Next
228+
current.Next = previous
229+
previous = current
230+
current = tempNext
231+
}
232+
233+
return previous
234+
}
136235
```
137236

138237
## Ruby
139238
```ruby
140-
# Welcome to create a PR to complete the code of this language, thanks!
239+
# class ListNode
240+
# attr_accessor :val, :next
241+
#
242+
# def initialize(val = 0, _next = nil)
243+
# @val = val
244+
# @next = _next
245+
# end
246+
# end
247+
248+
def reverse_list(head)
249+
previous = nil
250+
current = head
251+
252+
while current
253+
temp_next = current.next
254+
current.next = previous
255+
previous = current
256+
current = temp_next
257+
end
258+
259+
previous
260+
end
141261
```
142262

143263
## C

0 commit comments

Comments
 (0)