Java算法之反转链表

Java算法之反转链表

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)

上一篇:javascript 两种方法输出斐波那契数列的索引值


下一篇:双向链表的一些操作(Java)