https://leetcode.cn/problems/remove-linked-list-elements/description/
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
ListNode* pcur=head;
ListNode* 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(newHead)
newTail->next=NULL;
return newHead;
}
思路
解题过程
复杂度
https://leetcode.cn/problems/reverse-linked-list/description/
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head) {
ListNode* pcur=head;
ListNode* newHead,*headNext;
newHead=headNext=NULL;
while(pcur)
{
ListNode* next=pcur->next;
if(newHead==NULL)
{
newHead=headNext=pcur;
headNext->next=NULL;
}
else
{
//头插
newHead=pcur;
newHead->next=headNext;
headNext=newHead;
}
pcur=next;
}
return newHead;
}
思路
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head) {
ListNode* prev=NULL;
ListNode* pcur=head;
while(pcur)
{
ListNode* next=pcur->next;
pcur->next=prev;
prev=pcur;
pcur=next;
}
return prev;
}
思路
解题过程
复杂度
https://leetcode.cn/problems/middle-of-the-linked-list/description/
typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head) {
//节点个数
ListNode*pcur=head;
int count=0;
while(pcur)
{
count++;
pcur=pcur->next;
}
int mid=count/2;
ListNode* midNode=head;
while(mid--)
{
midNode=midNode->next;
}
return midNode;
}
思路
复杂度
typedef struct ListNode ListNode;
struct ListNode* middleNode(struct ListNode* head) {
ListNode* slow=head;
ListNode* fast=head;
while(fast!=NULL && fast->next!=NULL)
{
slow=slow->next;
fast=fast->next->next;
}
return slow;
}
思路
复杂度
https://leetcode.cn/problems/merge-two-sorted-lists/description/
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
ListNode*l1,*l2,*l3;
l1=list1;
l2=list2;
ListNode*newHead,*newTail;
newHead=newTail=(ListNode*)malloc(sizeof(ListNode));
//后面会用到newTail的next指针,所以需要初始化
newTail->next=NULL;
//l1用来遍历List1,l2用来遍历List2
while(l1!=NULL && l2!=NULL)
{
if(l1->val<l2->val)
{
newTail->next=l1;
newTail=newTail->next;
l1=l1->next;
}else{
newTail->next=l2;
newTail=newTail->next;
l2=l2->next;
}
}
//遍历完l2跳出循环
if(l1)
{
//将newTail的next指针指向l1可直接将l1后面的节点连接起来
newTail->next=l1;
}
//遍历完l1跳出循环
if(l2)
{
//将newTail的next指针指向l2可直接将l2后面的节点连接起来
newTail->next=l2;
}
ListNode* retNode=newHead->next;
//申请完空间要释放
free(newHead);
newHead=NULL;
return retNode;
}
思路
复杂度
https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
ListNode* lessHead,*lessTail;
lessHead=lessTail=(ListNode*)malloc(sizeof(ListNode));
ListNode* greaterHead,* greaterTail;
greaterHead=greaterTail=(ListNode*)malloc(sizeof(ListNode));
ListNode* pcur=pHead;
while(pcur)
{
if(pcur->val<x)
{
lessTail->next=pcur;
lessTail=lessTail->next;
}
else{
greaterTail->next=pcur;
greaterTail=greaterTail->next;
}
pcur=pcur->next;
}
greaterTail->next=NULL;
lessTail->next=greaterHead->next;
ListNode* ret=lessHead->next;
free(lessHead);
lessHead=NULL;
return ret;
}
};
思路
复杂度
本篇博客深入解析了经典的链表OJ习题,旨在帮助读者掌握链表操作的核心技巧与解题思路,如快慢指针、双指针等。通过对典型例题的剖析,助你巩固数据结构基础。
如果大家在练习中发现新的解题角度,或是有没搞懂的知识点,欢迎在评论区留言讨论。后续我也会持续更新数据结构相关的 OJ 解析,咱们一起在刷题中夯实基础,稳步进阶!