移除元素
题目连接放这了https://leetcode.cn/problems/remove-element/
思路一
创建一个新数组:首先遍历原数组的所有数据,把不等于val的值直接放在新数组里,然后返回新数组的长度。由于这个思路不符合题目的要求,所以我们不采用这个解法,这个思路仅作拓展!!!
int removeElement(int* nums, int numsSize, int val)
{
int n1, n2;
n1 = n2 = 0;
int arr[numsSize];
while (n2 < numsSize)
{
if (nums[n2] != val)
{
arr[n1] = nums[n2];
n1++;
}
n2++;
}
return n1;
}
思路二
使用两个临时变量n1和n2,n2负责遍历数组,n1负责将不等于val的值重新赋值给数组,最后n1就是新数组的下标
这是原数组:
开始进行遍历和赋值操作:
int removeElement(int* nums, int numsSize, int val)
{
int n1, n2;
n1 = n2 = 0;
while(n2 < numsSize)
{
if(nums[n2] != val)
{
nums[n1] = nums[n2];
n1++;
}
n2++;
}
return n1;
}
合并两个有序数组
题目连接放这了https://leetcode.cn/problems/merge-sorted-array/
思路
由于数组nums1有足够的空间,所以我们选择将两个合并的数组就直接放在数组nums1中,现在我们要考虑如何存放,由于两个数组是有序的并且是递增的,存放也是要求递增,所以我们要么从两个数组的首元素开始遍历比较进行存放,要么从两个数组的尾元素开始遍历比较进行存放。
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
int l1, l2, l3;
l1 = m - 1;
l2 = n - 1;
l3 = m + n - 1;
while (l1 >= 0 && l2 >= 0)
{
if (nums1[l1] > nums2[l2])
{
nums1[l3--] = nums1[l1--];
}
else
{
nums1[l3--] = nums2[l2--];
}
}
while (l2 >= 0)
{
nums1[l3--] = nums2[l2--];
}
}
移除链表元素
题目连接:https://leetcode.cn/problems/remove-linked-list-elements/
思路一
遍历原链表,将val的节点就地删除。
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val)
{
if (head == NULL)
{
return head;
}
ListNode* prev, * pcur;
prev = pcur = head;
ListNode* next;
while (pcur)
{
if (pcur->val == val)
{
next = pcur->next;
prev->next = next;
pcur = next;
}
else
{
prev = pcur;
pcur = pcur->next;
}
}
if (head->val == val)
{
return head->next;
}
return head;
}
思路二
创建新的链表,将不含val 的节点进行尾插即可。
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val)
{
if (head == NULL)
{
return head;
}
ListNode* NewHead, * NewTail;
NewHead = NewTail = (ListNode*)malloc(sizeof(ListNode));
ListNode* ptail = head;
while (ptail)
{
if (ptail->val != val)
{
//尾插
NewTail->next = ptail;
NewTail = NewTail->next;
}
ptail = ptail->next;
}
NewTail->next = NULL;
ListNode* next = NewHead->next;
free(NewHead);
return next;
}
反转链表
题目连接:https://leetcode.cn/problems/reverse-linked-list/
思路一
创建新链表,使用头插操作就可以实现链表的反转。
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL)
{
return head;
}
ListNode* NewHead, * NewTail;
NewHead = NewTail = (ListNode*)malloc(sizeof(ListNode));
NewTail->next = NULL;//先置为空,便于最后一个节点指向NULL
ListNode* ptail = head;
ListNode* next;
while (ptail)
{
next = ptail->next;//保存下一个节点
NewTail = NewHead->next;//保存新链表原先的第一个节点
NewHead->next = ptail;//头插
ptail->next = NewTail;//将新的第一个节点和原先的第一个节点进行连接
ptail = next;//继续遍历
}
next = NewHead->next;
free(NewHead);
return next;
}
思路二
原链表进行操作,改变每一个节点的指向,如下图所示:
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL)
{
return head;
}
ListNode* l1, * l2;
l1 = head;
l2 = head->next;
l1->next = NULL;//先将第一个节点的指向置为NULL
while (l2)
{
ListNode* next = l2->next;//保存下一个节点
l2->next = l1;
l1 = l2;
l2 = next;
}
return l1;
}
合并两个有序的链表
题目链接:https://leetcode.cn/problems/merge-two-sorted-lists/
思路
创建新链表,遍历两个链表的所有的节点,然后尾插到新链表里。
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
ListNode* NewHead, * NewTail;
NewHead = NewTail = (ListNode*)malloc(sizeof(ListNode));
NewHead->next = NULL;//处理空链表的情况
ListNode* l1, * l2;
l1 = list1;
l2 = list2;
while (l1 && l2)
{
if (l1->val < l2->val)
{
NewTail->next = l1;
l1 = l1->next;
NewTail = NewTail->next;
}
else
{
NewTail->next = l2;
l2 = l2->next;
NewTail = NewTail->next;
}
}
if (l1)
{
NewTail->next = l1;
}
if (l2)
{
NewTail->next = l2;
}
ListNode* next = NewHead->next;
free(NewHead);
return next;
}
链表的中间节点
题目连接:https://leetcode.cn/problems/middle-of-the-linked-list/
思路
typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head)
{
ListNode* slow, * fast;
slow = fast = head;
while (fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
环形链表的约瑟夫问题
题目链接:https://www.nowcoder.com/share/jump/9257752291712999799127
思路
这里我们可以使用循环链表来进行操作,循环链表是指头尾节点是相连的:
typedef struct ListNode ListNode;
ListNode* BuyNewnode(int x)//创建新节点
{
ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
if (newnode == NULL)
{
perror("malloc fail");
exit(1);
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
ListNode* CreatList(int n)//创建循环链表
{
ListNode* head, * tail;
head = BuyNewnode(1); //创建第一个节点
tail = head;
for (int i = 2; i <= n; i++)
{
ListNode* newnode = BuyNewnode(i);
tail->next = newnode;
tail = tail->next;
}
tail->next = head;//首尾相连
return tail;
}
int ysf(int n, int m)
{
int count = 1;
ListNode* pcur, * prev;
//创建循环链表
prev = CreatList(n);
pcur = prev->next;
while (prev != prev->next)
{
if (count == m)//删除节点
{
prev->next = pcur->next;
free(pcur);
pcur = prev->next;
count = 1;
}
else
{
prev = pcur;
pcur = pcur->next;
count++;
}
}
int ret = prev->val;
free(prev);
return ret;
}
分割链表
题目链接:https://leetcode.cn/problems/partition-list-lcci/
思路:
typedef struct ListNode ListNode;
struct ListNode* partition(struct ListNode* head, int x)
{
if (head == NULL)
{
return head;
}
ListNode* LessHead, * LessTail;
ListNode* BigHead, * BigTail;
//创建哨兵位
LessHead = LessTail = (ListNode*)malloc(sizeof(ListNode));
BigHead = BigTail = (ListNode*)malloc(sizeof(ListNode));
ListNode* ptail = head;
while (ptail)
{
if (ptail->val < x)
{
LessTail->next = ptail;
LessTail = LessTail->next;
}
else
{
BigTail->next = ptail;
BigTail = BigTail->next;
}
ptail = ptail->next;
}
BigTail->next = NULL;//将大连表的尾节点置为NULL
LessTail->next = BigHead->next;
return LessHead->next;
}