目录
1、反转链表:反转链表_牛客题霸_牛客网
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* ReverseList(struct ListNode* pHead ) {
if (pHead == NULL) {
return NULL;
}
struct ListNode *tmp = pHead->next;
struct ListNode *out = pHead;
out->next = NULL;
while (tmp) {
pHead = tmp;
tmp = pHead->next;
pHead->next = out;
out = pHead;
}
return out;
}
2、判断链表是否有环:判断链表中是否有环_牛客题霸_牛客网
bool hasCycle(struct ListNode *head) {
struct ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
return true;
}
}
return false;
}
3、跳台阶 跳台阶_牛客题霸_牛客网
int jumpFloor(int number ) {
// write code here
int f0 = 1, f1 = 1, res = 0;
if (number == 1)
return 1;
for (int i = 2; i <= number; ++i) {
res = f0 + f1;
f0 = f1;
f1 = f2;
}
return f2;
}
4、合并有序数组:合并两个有序的数组_牛客题霸_牛客网
void merge(int* A, int ALen, int m, int* B, int BLen, int n) {
int index = ALen + BLen - 1;
int i = ALen - 1, j = BLen - 1;
while (i >= 0 && j >= 0) {
if (A[i] < B[j]) {
A[index--] = B[j--];
} else {
A[index--] = A[i--];
}
}
while(j >= 0) {
A[index--] = B[j--];
}
}