💬 欢迎讨论:如果你在学习过程中有任何问题或想法,欢迎在评论区留言,我们一起交流学习。你的支持是我继续创作的动力! 👍 点赞、收藏与分享:觉得这篇文章对你有帮助吗?别忘了点赞、收藏并分享给更多的小伙伴哦!你们的支持是我不断进步的动力! 🚀 分享给更多人:如果你觉得这篇文章对你有帮助,欢迎分享给更多对C++感兴趣的朋友,让我们一起进步!
不熟悉顺序表的可以先了解一下 顺序表实现方法
给你一个数组
nums和一个值val,你需要 原地 移除所有数值等于val的元素。元素的顺序可能发生改变。然后返回nums中与val不同的元素的数量。 假设nums中不等于val的元素数量为k,要通过此题,您需要执行以下操作:
nums 数组,使 nums 的前 k 个元素包含不等于 val 的元素。nums 的其余元素和 nums 的大小并不重要。k。很容易想到的是用双指针进行遍历和赋值即可
int removeElement(int* nums, int numsSize, int val) {
int left = 0;
for (int right = 0; right < numsSize; right++) {
if (nums[right] != val) {
nums[left] = nums[right];
left++;
}
}
return left;
}while结束条件不要少了等于
int removeElement(int* nums, int numsSize, int val) {
int left = 0, right = numsSize-1;
while (left <= right) {
if (nums[left] == val) {
nums[left] = nums[right];
right--;
} else {
left++;
}
}
return left;
}注意:返回的是元素的个数
给你一个 非严格递增排列 的数组
nums,请你** 原地** 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回nums中唯一元素的个数。 考虑nums的唯一元素的数量为k,你需要做以下事情确保你的题解可以被通过:
nums ,使 nums 的前 k 个元素包含唯一元素,并按照它们最初在 nums 中出现的顺序排列。nums 的其余元素与 nums 的大小不重要。kint removeDuplicates(int* nums, int numsSize) {
int src = 0;
int dest = 1;
while (dest < numsSize) {
if(nums[src]!=nums[dest])
{
nums[++src]=nums[dest];
}
dest++;
}
return ++src;
}给你两个按 非递减顺序 排列的整数数组
nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。 请你 合并nums2到nums1中,使合并后的数组同样按 非递减顺序 排列。 **注意:**最终,合并后数组不应由函数返回,而是存储在数组nums1中。为了应对这种情况,nums1的初始长度为m + n,其中前m个元素表示应合并的元素,后n个元素为0,应忽略。nums2的长度为n。
int cmp(int* a, int* b) {
return *a - *b;
}
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
for (int i = 0; i != n; ++i) {
nums1[m + i] = nums2[i];
}
qsort(nums1, nums1Size, sizeof(int), cmp);
}void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n) {
int l1=nums1Size-1;
int l2=m-1;
int l3=n-1;
while(l2>=0&&l3>=0)
{
if(nums1[l2]>nums2[l3])
{
nums1[l1]=nums1[l2];
l2--;
}
else
{
nums1[l1]=nums2[l3];
l3--;
}
l1--;
}
if(l2<0)
{
while(l1>=0)
nums1[l1--]=nums2[l3--];
}
}不熟悉链表的可以先了解一下 单链表实现方法
给你一个链表的头节点
head和一个整数val,请你删除链表中所有满足Node.val == val的节点,并返回 新的头节点 。

- 最直观的办法就是创建一个新链表
注:不是开辟空间的深拷贝,而只是定义了指向同一结点的指针
在最后需要先判断newtail是否为空(否则链表为空链表时会报错)再将其中的next指针置为空(否则可能会出现循环)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
ListNode* removeElements(ListNode* head, int val) {
ListNode* pcur = head;
ListNode* newhead = NULL;
ListNode* newtail = NULL;
while (pcur) {
if (pcur->val != val) {
if (newhead == NULL) {
newhead = newtail = pcur;
} else {
newtail->next = pcur;
newtail = newtail->next;
}
}
pcur = pcur->next;
}
if (newtail)
newtail->next = NULL;
return newhead;
}struct ListNode* removeElements(struct ListNode* head, int val) {
if (head == NULL) {
return head;
}
head->next = removeElements(head->next, val);
return head->val == val ? head->next : head;
}给你单链表的头节点
head,请你反转链表,并返回反转后的链表。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head) {
if(head==NULL)
return head;
ListNode* n1,*n2,*n3;
n1=NULL,n2=head,n3=head->next;
while(n3){
n2->next=n1;
n1=n2;
n2=n3;
n3=n3->next;
}
n2->next=n1;
return n2;
}给你单链表的头结点
head,请你找出并返回链表的中间结点。 如果有两个中间结点,则返回第二个中间结点

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head) {
ListNode* slow, *fast;
slow=fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
ListNode* newhead,*newtail;
newhead=newtail=(ListNode*)malloc(sizeof(ListNode));
while(list1&&list2)
{
if(list1->val<list2->val)
{
newtail->next=list1;
list1=list1->next;
}
else
{
newtail->next=list2;
list2=list2->next;
}
newtail=newtail->next;
}
if(list1)
{
newtail->next=list1;
}
else
{
newtail->next=list2;
}
ListNode* ret=newhead->next;
free(newhead);
newhead=newtail=NUll;
return ret;
}有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针
ListNode* partition(ListNode* pHead, int x) {
if(pHead == NULL)
return NULL;
struct ListNode* lessHead, *lessTail,*greaterHead, *greaterTail;
//创建链表表头
lessHead = lessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
greaterHead = greaterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* cur = pHead;
while(cur)
{
//小于x的尾插到lessTail
if(cur->val < x)
{
lessTail->next = cur;
lessTail = lessTail->next;
}
//大于等于x的尾插到greaterTail
else
{
greaterTail->next = cur;
greaterTail = greaterTail->next;
}
cur = cur->next;
}
//链接两个链表
lessTail->next = greaterHead->next;
greaterTail->next = NULL;
//获取表头
pHead = lessHead->next;
free(lessHead);
free(greaterHead);
return pHead;
}对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。 给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900
bool chkPalindrome(ListNode* A) {
// write code here
int a[900] = {0};
ListNode* cur = A;
int n = 0;
//保存链表元素
while(cur)
{
a[n++] = cur->val;
cur = cur->next;
}
//判断数组是否为回文结构
int begin = 0, end = n-1;
while(begin < end)
{
if(a[begin] != a[end])
return false;
++begin;
--end;
}
return true;
}bool chkPalindrome(ListNode* A) {
if (A == NULL || A->next == NULL)
return true;
ListNode* slow, *fast, *prev, *cur, *nxt;
slow = fast = A;
//找到中间节点
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
prev = NULL;
//后半部分逆置
cur = slow;
while (cur)
{
nxt = cur->next;
cur->next = prev;
prev = cur;
cur = nxt;
}
//逐点比对
while (A && prev)
{
if (A->val != prev->val)
return false;
A = A->next;
prev = prev->next;
}
return true;
}以上就是顺序表和链表的一些算法题啦啦,各位大佬有什么问题欢迎在评论区指正,您的支持是我创作的最大动力!❤️