今天这道题是道简单题,使用双指针进行迭代即可,画了下草图如下
代码如下
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)全球极客挚爱的技术成长平台