Skip to content

Commit c8db928

Browse files
[refactor] change to md format
1 parent 5bd50ac commit c8db928

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

206. Reverse Linked List.cpp renamed to 206. Reverse Linked List.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/**
1+
## Problem description
2+
23
Reverse a singly linked list.
34

45
Example:
@@ -8,8 +9,10 @@ Output: 5->4->3->2->1->NULL
89
Follow up:
910

1011
A linked list can be reversed either iteratively or recursively. Could you implement both?
11-
**/
1212

13+
## Iterative
14+
15+
```cpp
1316
//iterative
1417
/**
1518
* Definition for singly-linked list.
@@ -37,12 +40,12 @@ class Solution {
3740
return prev;
3841
}
3942
};
43+
```
4044
45+
## Recursive
46+
The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part?
4147
42-
//recursive
43-
/**
44-
Approach #2 (Recursive) [Accepted]
45-
The recursive version is slightly trickier and the key is to work backwards. Assume that the rest of the list had already been reversed, now how do I reverse the front part? Let's assume the list is: n1 → … → nk-1 → nk → nk+1 → … → nm → Ø
48+
Let's assume the list is: n1 → … → nk-1 → nk → nk+1 → … → nm → Ø
4649
4750
Assume from node nk+1 to nm had been reversed and you are at node nk.
4851
@@ -57,9 +60,7 @@ nk.next.next = nk;
5760
Be very careful that n1's next must point to Ø.
5861
If you forget about this, your linked list has a cycle in it.
5962
This bug could be caught if you test your code with a linked list of size 2.
60-
**/
6163
62-
/**
6364
Complexity analysis
6465
6566
Time complexity : O(n).
@@ -68,13 +69,13 @@ Assume that nn is the list's length, the time complexity is O(n).
6869
Space complexity : O(n).
6970
The extra space comes from implicit stack space due to recursion.
7071
The recursion could go up to nn levels deep.
71-
**/
7272
73-
/**
73+
7474
Runtime: 8 ms, faster than 100.00% of C++ online submissions for Reverse Linked List.
75+
7576
Memory Usage: 9.3 MB, less than 10.03% of C++ online submissions for Reverse Linked List.
76-
**/
77-
/**
77+
78+
```cpp
7879
class Solution {
7980
public:
8081
ListNode* reverseList(ListNode* head) {
@@ -87,4 +88,4 @@ class Solution {
8788
return p;
8889
}
8990
};
90-
**/
91+
```

0 commit comments

Comments
 (0)