⽬录
1.
单链表经典算法OJ题⽬
1.1 单链表相关经典算法OJ题1:移除链表元素
203. 移除链表元素 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* removeElements(struct ListNode* head, int val) {
LN* newhead,* newtail;
newhead=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;
}
1.2 单链表相关经典算法OJ题2:206. 反转链表 - 力扣(LeetCode)
思路1.创建一个新i链表然后头插
思路2.创建三个指针,分别定义前驱节点,当前节点,后续节点,来反转链表具体实现如下,
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* reverseList(struct ListNode* head) {
if(head==NULL)
{
return head;
}
LN* n1,*n2,*n3;
n1=NULL;
n2=head;
n3=head->next;
LN* pcur=head;
while(n2)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3)
{
n3=n3->next;
}
}
return n1;
}
1.3 单链表相关经典算法OJ题3:21. 合并两个有序链表 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
if(list1==NULL)
{
return list2;
}
else if(list2==NULL)
{
return list1;
}
else if(list1==NULL && list2==NULL)
{
return NULL;
}
LN* l1=list1;
LN* l2=list2;
LN* newNode,* newTail;
newNode=newTail=NULL;
while(l1 && l2)
{
if(l1->val<l2->val)
{
if(newNode==NULL)
{
newNode=newTail=l1;
}
else
{
newTail->next=l1;
newTail=newTail->next;
}
l1=l1->next;
}
else
{
if(newNode==NULL)
{
newNode=newTail=l2;
}
else
{
newTail->next=l2;
newTail=newTail->next;
}
l2=l2->next;
}
}
if(l1)
{
newTail->next=l1;
}
if(l2)
{
newTail->next=l2;
}
return newNode;
}
通过上面代码可以发现有大部分重复的代码,在原来的基础上还是可以优化的
通过上面可以发现这些代码很多重复的都在判断节点是不是空的所以我们这里采用带头的单链表这样就可以省去判断的时间
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
if(list1==NULL)
{
return list2;
}
else if(list2==NULL)
{
return list1;
}
else if(list1==NULL && list2==NULL)
{
return NULL;
}
LN* l1=list1;
LN* l2=list2;
LN* newNode,* newTail;
newNode=newTail=(LN*)malloc(sizeof(LN));
while(l1 && l2)
{
if(l1->val<l2->val)
{
newTail->next=l1;
newTail=newTail->next;
l1=l1->next;
}
else
{
newTail->next=l2;
newTail=newTail->next;
l2=l2->next;
}
}
if(l1)
{
newTail->next=l1;
}
if(l2)
{
newTail->next=l2;
}
return newNode->next;
}
1.4 单链表相关经典算法OJ题4876. 链表的中间结点 - 力扣(LeetCode)
快慢指针:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* middleNode(struct ListNode* head) {
LN* fast,* slow;
fast=slow=head;
while(fast && fast->next)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
1.5 循环链表经典应⽤-环形链表的约瑟夫问题_牛客题霸_牛客网 (nowcoder.com)
据说著名犹太
历史学家
Josephus有过以下的故事:在罗⻢⼈占领乔塔帕特后,39 个犹太⼈与
Josephus及他的朋友躲到⼀个洞中,39个犹太⼈决定宁愿死也不要被⼈抓到,于是决定了⼀个⾃杀 ⽅式,41个⼈排成⼀个圆圈,由第1个⼈开始报数,每报数到第3⼈该⼈就必须⾃杀,然后再由⼀ 个重新报数,直到所有⼈都⾃杀⾝亡为⽌。
然⽽Josephus 和他的朋友并不想遵从,Josephus要他的朋友先假装遵从,他将朋友与⾃⼰安排在
第16个与第31个位置,于是逃过了这场死亡游戏。
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @param m int整型
* @return int整型
*/
#include <stdlib.h>
typedef struct ListNode LN ;
LN* BuyNode(int x)
{
LN* newnode=(LN*)malloc(sizeof(LN));
newnode->val=x;
newnode->next=NULL;
return newnode;
}
LN* create(int n)
{
LN* phead=BuyNode(1);
LN* ptail=phead;
for (int i=2; i<=n; i++)
{
ptail->next=BuyNode(i);
ptail=ptail->next;
}
ptail->next=phead;
return ptail;
}
int ysf(int n, int m ) {
LN* prev=create(n);
LN* pcur=prev->next;
int count=1;
while(pcur->next!=pcur)
{
if(count==m)
{
prev->next=pcur->next;
pcur=pcur->next;
count=1;
}
else
{
prev=pcur;
pcur=pcur->next;
count++;
}
}
return pcur->val;
}
1.6 单链表相关经典算法OJ题5:分割链表
面试题 02.04. 分割链表 - 力扣(LeetCode)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode LN;
struct ListNode* partition(struct ListNode* head, int x){
if(head==NULL)
{
return head;
}
LN* bigHead=(LN*)malloc(sizeof(LN));
LN* bigTail=bigHead;
LN* smallHead=(LN*)malloc(sizeof(LN));
LN* smallTail=smallHead;
LN* pcur=head;
while(pcur)
{
if(pcur->val<x)
{
smallTail->next=pcur;
smallTail=smallTail->next;
}
else
{
bigTail->next=pcur;
bigTail=bigTail->next;
}
pcur=pcur->next;
}
bigTail->next=NULL;
smallTail->next=bigHead->next;
return smallHead->next;
}