- 博客(19)
- 收藏
- 关注
原创 20.有效的括号
力扣思路是:创建栈,左括号入栈,右括号对比。typedef char STDatatype;typedef struct stack{ STDatatype* a; int top; int capacity;}ST;void StackInit(ST* ps);void StackDestory(ST* ps);void StackPush(ST* ps,STDatatype x);void StackPop(ST* ps);STDatatype StackTop(
2022-05-31 09:33:22
9
原创 138.复制带随机指针的链表
力扣1.复制结点2.控制random3.尾插struct Node* copyRandomList(struct Node* head){ //1.复制结点 struct Node* cur = head; while(cur) { //创建结点 struct Node* copy =(struct Node*)malloc(sizeof(struct Node)); copy->val = cur-&
2022-05-16 19:48:09
8
原创 CC7 牛牛的单向链表
牛牛的单向链表_牛客题霸_牛客网思路:创建结点,建立哨兵位,输入,打印。#include<stdio.h>#include<stdlib.h>#include<assert.h>typedef struct list{ int val; struct list* next;}list;list* Buynode(int x){ list* node = (list*)malloc(sizeof(list));
2022-05-16 19:23:12
原创 142.环形链表
力扣一个指针从 meet 走,一个从 head 走,一定会在入口相遇struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *slow = head; struct ListNode *fast = head; while(fast && fast->next) { slow = slow->next; fast =
2022-05-13 22:57:58
10
原创 141.环形链表
力扣思路是:快慢指针,如何有环,两个指针进入环一定可以追上。/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) { struct ListNode *slow = head; struct ListNode *fas..
2022-05-11 20:16:29
7
原创 160.相交链表
力扣思路是:遍历两个链表,算出各自的长度,再看看尾结点相不相同。如果不相同,就没有相交。如果相同,让长的链表先走差距步,到同一起跑线,开始找相交结点。/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ //记住不是比较 val //比较的是结点的地址 struct ListNode *getInte
2022-05-11 19:56:42
10
原创 234.回文链表
力扣/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; *///找中间 struct ListNode* middlenode(struct ListNode* head) { struct ListNode* slow = head; struct ListNode* f
2022-05-11 16:30:22
10
原创 链表的回文结构
链表的回文结构_牛客题霸_牛客网思路:找到中间结点,逆置后半部分,逆置后与前半部分进行比较。借用的是之前的代码(找中间结点,逆置链表)/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {}};*/class PalindromeList {public: //找中间 struct ListNode* middlen
2022-05-11 16:18:48
原创 链表的分割
链表分割_牛客题霸_牛客网创建两个链表,进行尾插,小的插一个,大的插一个。要考虑极端条件,避免形成环。class Partition {public: ListNode* partition(ListNode* pHead, int x) { struct ListNode*greaterhead,*greatertail,*lesshead,*lesstail; greaterhead = greatertail =(struct List
2022-05-09 12:17:32
原创 21. 合并两个有序链表
力扣归并思路 :建立哨兵位,让 list1 中的值 和 list2 中的值进行比较,小的尾插。struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){ struct ListNode* head; struct ListNode* tail; head = tail =(struct ListNode*)malloc(sizeof(struct ListNode).
2022-05-08 18:53:03
10
原创 链表中倒数第k个结点
链表中倒数第k个结点_牛客题霸_牛客网采用快慢指针struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) { struct ListNode* slow = pListHead; struct ListNode* fast = pListHead; while(k) { fast = fast->next; k--; } whi
2022-05-08 15:33:55
原创 876.链表的中间结点
力扣采用快慢指针struct ListNode* middleNode(struct ListNode* head){ struct ListNode* slow = head; struct ListNode* fast = head; while(fast && fast->next) { slow = slow->next; fast = fast->next->next; .
2022-05-08 14:48:45
12
原创 206.反转链表
力扣法1 采用头插的方法解题,最后返回新的头。struct ListNode* reverseList(struct ListNode* head){ struct ListNode* newhead = NULL; struct ListNode* cur = head; while(cur) { struct ListNode* next = cur->next; //记录下一个 cur->next.
2022-05-08 10:05:11
12
原创 203.移除链表元素
力扣法 1 用删除的思路struct ListNode* removeElements(struct ListNode* head, int val){ struct ListNode *cur = head; struct ListNode *prev = NULL; while(cur != NULL) { if(cur->val == val) { if(cur == head) .
2022-05-06 09:39:20
8
原创 27.移除元素
力扣注意条件 : 原地修改在原数组覆盖 (时间:n 空间:1)int removeElement2(int* nums, int numsSize, int val){ int i; int a = 0; int t = 0; for (i = 0; i < numsSize; i++) { if (nums[i] == val) { a++; } else { nums[t++] = nums[a++]; } } r..
2022-05-04 11:53:51
8
原创 88.合并两个有序数组
力扣思路是 nums1 数组是无限大的,将 nums2 中的数字填入 nums1 中,利用非递减序列的特点,从后往前填入数字。 当end 2 < 0,说明已经完成合并。当end 1 < 0 时,说明 nums2 中的数字小,跳出第一个循环,第二个循环就是填入 nums1 中。void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){ int end1 = m - 1.
2022-04-26 19:24:06
3
原创 面试题 17.04.消失的数字
力扣法 1思路是先是从0加到n,得到 sum,再将数组中的内容相加,得到 sum1。用sum - sum1 就得到了消失的数字int missingNumber(int* nums, int numsSize){ int sum = 0; int sum1 = 0; int i; for (i = 0; i <= numsSize; i++) { sum = sum + i; } for (i = 0; i.
2022-04-25 20:06:04
9
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人