@@ -25,39 +25,11 @@ A linked list can be reversed either iteratively or recursively. Could you imple
2525
2626## 代码
2727
28- 语言支持:JS, C++, Python
28+ 语言支持:JS, C++, Python,Java
2929
3030JavaScript Code:
3131
3232``` js
33- /*
34- * @lc app=leetcode id=206 lang=javascript
35- *
36- * [206] Reverse Linked List
37- *
38- * https://leetcode.com/problems/reverse-linked-list/description/
39- *
40- * algorithms
41- * Easy (52.95%)
42- * Total Accepted: 532.6K
43- * Total Submissions: 1M
44- * Testcase Example: '[1,2,3,4,5]'
45- *
46- * Reverse a singly linked list.
47- *
48- * Example:
49- *
50- *
51- * Input: 1->2->3->4->5->NULL
52- * Output: 5->4->3->2->1->NULL
53- *
54- *
55- * Follow up:
56- *
57- * A linked list can be reversed either iteratively or recursively. Could you
58- * implement both?
59- *
60- */
6133/**
6234 * Definition for singly-linked list.
6335 * function ListNode(val) {
@@ -134,6 +106,33 @@ class Solution:
134106 return prev
135107```
136108
109+ Java Code:
110+
111+ ``` java
112+ /**
113+ * Definition for singly-linked list.
114+ * public class ListNode {
115+ * int val;
116+ * ListNode next;
117+ * ListNode(int x) { val = x; }
118+ * }
119+ */
120+ class Solution {
121+ public ListNode reverseList (ListNode head ) {
122+ ListNode pre = null , cur = head;
123+
124+ while (cur != null ) {
125+ ListNode next = cur. next;
126+ cur. next = pre;
127+ pre = cur;
128+ cur = next;
129+ }
130+
131+ return pre;
132+ }
133+ }
134+ ```
135+
137136## 拓展
138137
139138通过单链表的定义可以得知,单链表也是递归结构,因此,也可以使用递归的方式来进行reverse操作。
0 commit comments