题目地址:https://leetcode-cn.com/problems/reverse-linked-list/
思路:用一个temp节点保存下一个的节点即可
AC代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode cur = head.next;
head.next = null;
ListNode temp;
while (cur != null) {
temp = cur.next;
cur.next = head;
head = cur;
cur = temp;
}
return head;
}
}