题目
代码部分一(0ms 迭代)
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode node, temp;
node = head.next;
head.next = null;
while(node.next != null){
temp = node.next;
node.next = head;
head = node;
node = temp;
}
node.next = head;
head = node;
return head;
}
}
代码部分二(0ms 递归)
class Solution {
ListNode node, temp;
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode res = reverseList(head.next);
head.next.next = head;
head.next = null;
return res;
}
}