/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null){
return head;
}
System.out.println(head.val);
ListNode tail = new ListNode(head.val);
head = head.next;
while(head != null){
tail.next = tail;
tail.val = head.val;
head = head.next;
}
return tail;
}
}
在Leetcode上我试图扭转一个单链表,这是一个问题,为什么它不能及时做到?
使用输入[1, 2]
,它显示超出时间限制,但我不明白为什么。有人可以帮忙吗?