We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 749087b commit f5ccee1Copy full SHA for f5ccee1
zh-hans/basics_data_structure/linked_list.md
@@ -96,6 +96,7 @@ class ListNode {
96
}
97
98
99
+// iterative method
100
public ListNode reverse(ListNode head) {
101
ListNode prev = null;
102
while (head != null) {
@@ -106,6 +107,18 @@ public ListNode reverse(ListNode head) {
106
107
108
return prev;
109
110
+
111
+// recursive method
112
+public ListNode reverse(ListNode head) {
113
+ if (head == null || head.next == null) {
114
+ return head;
115
+ }
116
+ ListNode next = head.next;
117
+ ListNode newHead = reverse(next);
118
+ next.next = head;
119
+ head.next = null;
120
+ return newHead;
121
122
```
123
124
#### 双向链表
0 commit comments