今天這道題是道簡單題,使用雙指針進行迭代即可,畫了下草圖如下
代碼如下
class Solution {public ListNode reverseList(ListNode head) {if (head == null || head.next == null) {return head;}ListNode p = head, q = head.next, temp = null;while (q != null) {p.next = temp;temp = p;p = q;q = q.next;}p.next = temp;return p;}
}
題目鏈接:題單 - 力扣(LeetCode)全球極客摯愛的技術成長平臺