逆序

单链表逆序、数组逆序

Posted by candy1126xx on June 12, 2017

单链表逆序

Node invertList(Node head) {
	if (head == null || head.next == null) 
		return head;
          
	Node newHead = null;  
	Node nextNode = null;  
          
	while (head != null) {  
		nextNode = head.next;  
		head.next = newHead;  
		newHead = head;  
		head = nextNode;  
	}  
  
	return newHead;  
}

数组逆序

int[] invertArray(int[] array) {
	int temp = 0;
	for (int start = 0, end = array.length - 1; start < end; start++, end--) {
        temp = array[end];
        array[end] = array[start];
        array[start] = temp;
    }
}