/**
給你單連結清單的頭節點 <code>head</code> ,請你反轉連結清單,并傳回反轉後的連結清單。
<div class="original__bRMd">
<div>
<p> </p>
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>輸入:</strong>head = [1,2,3,4,5]
<strong>輸出:</strong>[5,4,3,2,1]
</pre>
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg" style="width: 182px; height: 222px;" />
<pre>
<strong>輸入:</strong>head = [1,2]
<strong>輸出:</strong>[2,1]
</pre>
<p><strong>示例 3:</strong></p>
<pre>
<strong>輸入:</strong>head = []
<strong>輸出:</strong>[]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>連結清單中節點的數目範圍是 <code>[0, 5000]</code></li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
<p> </p>
<p><strong>進階:</strong>連結清單可以選用疊代或遞歸方式完成反轉。你能否用兩種方法解決這道題?</p>
</div>
</div>
<div><div>Related Topics</div><div><li>遞歸</li><li>連結清單</li></div></div><br><div><li>???? 2453</li><li>???? 0</li></div>
*/
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
//不知道該說什麼,大佬牛逼
//每一次調轉前後指針的順序,從最後倆個開始
public ListNode reverseList(ListNode head) {
if(head == null || head.next==null){
return head;
}
ListNode last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
}
//leetcode submit region end(Prohibit modification and deletion)
不會,我可以學;落後,我可以追趕;跌倒,我可以站起來!