public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } class Solution { public ListNode reverseList(ListNode head) { ListNode prve = null; ListNode curr = head; while(curr != null){ ListNode next = curr.next; curr.next = prve; prve = curr; curr = next; } return prve; } }
时间复杂度:O(n)
空间复杂度:O(1)