int binarySearch(int array[],int length,int target)//array 为一个排好序的数组
{
if(length/2==0 && array[length/2] != target )return -1;
if(array[length/2]==target)return length/2;
if(array[length/2]>target])return binarySearch(array[],length/2,target);
if(array[length/2]<target])return binarySearch(array[],length/2,length-length/2);
}
typedef struct Node Node;
struct Node{
int data;
Node* next;
};
Node* reverseLinkList(Node* head){
if (head==NULL||head->next==NULL) {
return head;
}
Node* p1 = head;
Node* p2 = p1->next;
Node* p3 = p2->next;
p1->next = NULL;//p1要变成尾指针,所以指向NULL
while (p3!=NULL) {
p2->next = p1;//p2->next 反向
p1=p2;//p1指针向后移动一步
p2=p3;//p2指针向后移动一步
p3=p3->next;//p3指针向后移动一步
}
p2->next=p1;//p3指向了NULL,p2反向
head=p2;//头指针指向之前的尾指针
return head;
}
网友评论