链表指定区间反转与分区策略
针对指定区间内的链表节点进行反转操作。
该问题的核心思路可视为将前后部分的节点断开,并重新组合连接。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
for (int i = 0; i < m - 1; ++i)
pre = pre->next;
ListNode *cur = pre->next;
for (int i = m; i < n; ++i) {
ListNode *t = cur->next;
cur->next = t->next;
t->next = pre->next;
pre->next = t;
}
return dummy->next;
}
};
对链表进行分割,使得小于特定值的节点位于前面。
解题策略是先定位第一个大于或等于给定阈值x的节点,随后逐个遍历后续节点,将所有小于x的值插入到该节点之前。例如,若先找到值为4的节点,再找到值为3的节点,则将小于3的所有值移至其前部。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *p=new ListNode(-1);
p->next=head;
ListNode *pre=p,*cur=head;
while(pre->next&&pre->next->valnext;
cur=pre;
while(cur->next){
if(cur->next->valnext;
cur->next=tmp->next;
tmp->next=pre->next;
pre->next=tmp;
pre=pre->next;
}else{
cur=cur->next;
}
}
return p->next;
}
};
逆序与顺序链表数字相加算法
处理以逆序方式存储的数字链表,计算两数之和。
首先创建一个虚拟头结点p,随后依次生成表示和的新节点并链接至p之后。使用指针cur指向新链表的尾部,并维护进位变量res。每次计算两节点值之和sum时,通过sum/10获取进位值,以sum%10构建新节点。需注意最高位可能产生的进位,因此while循环结束后,若res仍为1,需额外创建一个值为1的节点追加至末尾。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p=new ListNode(-1),*cur=p;
int res=0;
while(l1||l2){
int val1=l1?l1->val:0;
int val2=l2?l2->val:0;
int sum=val1+val2+res;
res=sum/10;
cur->next=new ListNode(sum%10);
cur=cur->next;
if(l1)
l1=l1->next;
if(l2)
l2=l2->next;
}
if(res)
cur->next=new ListNode(1);
return p->next;
}
};
处理以顺序方式存储的数字链表,计算两数之和。
思路:这道题和第2题类似,但是链表是从前往后遍历,加法却要从最低位相加,所以可以考虑改用栈来存储放进来的数据。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack s1, s2;
while (l1) {
s1.push(l1->val);
l1 = l1->next;
}
while (l2) {
s2.push(l2->val);
l2 = l2->next;
}
int sum = 0;
ListNode *res = new ListNode(0);
while (!s1.empty() || !s2.empty()) {
if (!s1.empty()) {
sum += s1.top();
s1.pop();
}
if (!s2.empty()) {
sum += s2.top();
s2.pop();
}
res->val = sum % 10;
ListNode *cur = new ListNode(sum / 10);
cur->next = res;
res = cur;
sum /= 10;
}
return res->val == 0 ? res->next : res;
}
};
链表元素移除与重复节点删除
从链表中移除所有包含特定值的节点。
采用递归方法直达链表末尾,在回溯过程中,若当前节点值需删除,则将其next指针直接指向下一个有效节点即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(!head) return NULL;
head->next=removeElements(head->next,val);
return head->val==val?head->next:head;
}
};
删除已排序链表中存在的重复元素,保留每个值只出现一次。
利用递归查找,若head节点值存在且后续节点值相等,则通过while循环跳过所有值相等的节点;若递归返回后仍有重复情况,继续处理。最终返回去重后的head。相比新建链表存储的方式,此方法时间复杂度更低。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head) return head;
if (head->next && head->val == head->next->val) {
while (head->next && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
}
head->next = deleteDuplicates(head->next);
return head;
}
};
删除未排序链表中存在的重复元素,确保每个值仅保留一个。
比较head节点与其后继节点的值,若相同则直接返回后继节点作为新的head。最后通过回溯递归调用,逐步删除重复节点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head||!head->next) return head;
head->next=deleteDuplicates(head->next);
return (head->val==head->next->val)?head->next:head;
}
};
以上内容涵盖了链表处理的多种常见场景,希望能为读者的学习提供有益参考,欢迎持续关注本站获取更多技术分享。