66 new = (struct ListNode *)malloc(sizeof(struct ListNode));
67 if (NULL == new) {
68 perror("malloc failed!\n");
69 return NULL;
70 }
71 tail1->next = new;
72 new->val = tmp;
73 new->next = NULL;
74 }
75 }
76
77 return l1;
78 }
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6] , 5 → 2
[1,3,5,6] , 2 → 1
[1,3,5,6] , 7 → 4
[1,3,5,6] , 0 → 0
1 int searchInsert(int* nums, int numsSize, int target) {
2 int *pNum = NULL;
3 int i = 0;
4
5 if (NULL == nums) {
6 return -1;
7 }
8
9 if (numsSize <= 0) {
10 return -1;
11 }
12
13 pNum = nums;
14
15 for (i = 0; i < numsSize; i++) {
16 if (*(pNum + i) >= target) {
17 return i;
18 } else {
19 if (i == numsSize - 1) {
20 return numsSize;
21 }
22 }
23 }
24
25 return -1;
26 }
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * struct ListNode *next;
6 * };
7 */
8 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
9 struct ListNode *tail1 = NULL;
10 struct ListNode *tail2 = NULL;
11 struct ListNode *bak_node1 = NULL;
12 struct ListNode *bak_node2 = NULL;
13 struct ListNode *bak_node = NULL;
14
15 if (l1 == NULL) {
16 return l2;
17 }
18
19 if (l2 == NULL) {
20 return l1;
21 }
22
23 // 找到第一个数据最小的链表,作为返回链表
24 if (l1->val <= l2->val) {
25 tail1 = l1;
26 tail2 = l2;
27 } else {
28 tail1 = l2;
29 tail2 = l1;
30 }
31 bak_node1 = tail1;
32 bak_node2 = tail2;
33
34 bak_node = tail1;
35
36 while (tail2 != NULL) {
37 while (tail1->val <= tail2->val) {
38 if (tail1->next == NULL) {
39 tail1->next = tail2;
40 return bak_node;
41 }
42 bak_node1 = tail1;
43 tail1 = tail1->next;
44 }
45
46 bak_node2 = tail2->next;
47 tail2->next = tail1;
48 bak_node1->next = tail2;
49 bak_node1 = bak_node1->next;
50 tail2 = bak_node2;
51 }
52
53 return bak_node;
54 }
1 bool isValid(char* s) {
2 char *pString = NULL;
3 int flag1 = 0, flag2 = 0, flag3 = 0;
4 int flag = 0;
5
6 if (s == NULL) {
7 perror("String NULL!\n");
8 return false;
9 }
10
11 pString = s;
12
13 while (*pString != '\0') {
14 if (*pString == '(') {
15 flag1++;
16 flag = 1;
17 } else if (*pString == ')') {
18 if (flag == 1 && flag1 != 0) {
19 flag1--;
20 } else if (flag == 0) {
21 return false;
22 }
23 }
24
25 if (*pString == '{') {
26 flag2++;
27 flag = 2;
28 } else if (*pString == '}') {
29 if (flag == 2 && flag2 != 0) {
30 flag2--;
31 } else if (flag == 0) {
32 return false;
33 }
34 }
35
36 if (*pString == '[') {
37 flag3++;
38 flag = 3;
39 } else if (*pString == ']') {
40 if (flag == 3 && flag3 != 0) {
41 flag3--;
42 } else if (flag == 0) {
43 return false;
44 }
45 }
46
47 if (flag1 + flag2 + flag3 > 1) {
48 return false;
49 }
50 pString++;
51 }
52
53 if ((flag1 != 0) || (flag2 != 0) || (flag3 != 0)) {
54 return false;
55 }
56
57 return true;
58 }
Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting |