1.指定区间反转
leetcode力扣 92https://leetcode.cn/problems/reverse-linked-list-ii/description/题目: 给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回反转后的链表 。
示例: 输入:head = [1,2,3,4,5] , left = 2 , right = 4
输出:head = [1,4,3,2,5]
1.1头插法
我们可以类比该题与基础的将整个链表反转,其实只不过是反转其中一部分而已,所以类比之前的利用虚拟头结点的方法,
反转的整体思想就是,在需要反转的区间内,每遍历到一个结点,都将这个结点变成反转区间的第一个结点,
例如下图: left = 3 , right = 6
其实整体过程与前面的建立虚拟结点的方法基本一致,但是在这里要找到,反转区间的前一个结点,
//头插法
ListNode* reverseBetween(ListNode* head, int left, int right) {
if(head==NULL) return NULL;
ListNode* dummy = new ListNode(-1,head);
ListNode* pre = dummy;
ListNode* cur = NULL;
ListNode* next = NULL;
int m=left;
//找到反转区间的前一个结点
while(m!=1){
pre=pre->next;
m--;
}
cur=pre->next;
//开始反转
for(m=0;m<right-left;m++){
next=cur->next;
cur->next=next->next;
next->next=pre->next;
pre->next=next;
}
cur=dummy->next;
delete dummy;
return cur;
}
1.2穿针引线法
穿针引线法就是直接在链表上操作,就是先找到反转区间,和区间前一个结点与后一个结点,然后直接改变反转区间链表每个结点中箭头的方向,最后将反转区间与区间前一个结点与后一个结点正确对接就可以了
下面看示意图:
反转前:
开始反转:
1.将反转区间的链表反转 2.将pre,succ与反转区间正确对接 //注意箭头的指向
//反转链表
ListNode* reverseList(ListNode* head) {
if(head==NULL) return NULL;
ListNode* cur=head;
ListNode* pre=NULL;
ListNode* next=NULL;
while(cur!=NULL){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
//穿针引线法
ListNode* reverseBetween(ListNode* head, int left, int right) {
if(head==NULL) return NULL;
ListNode* dummy = new ListNode(-1,head);
ListNode* pre = dummy;
ListNode* succ = NULL;
ListNode* leftNode = NULL;
ListNode* rightNode = NULL;
int m=left;
//找到反转区间的前一个结点
while((m-1)!=0){
pre=pre->next;
m--;
}
leftNode=pre->next;
rightNode=leftNode;
//找到反转区间的最后一个结点
for(m=0;m<right-left;m++){
rightNode=rightNode->next;
}
succ=rightNode->next;
rightNode->next=NULL;
//将反转区间反转
reverseList(leftNode);
//将反转区间正确接入
pre->next=rightNode;
leftNode->next=succ;
pre=dummy->next;
delete dummy;
return pre;
}
2.两两交换链表中的结点
leetcode力扣 24https://leetcode.cn/problems/swap-nodes-in-pairs/description/
这是一道非常重要的题,务必理解清楚
题目:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
示例: 输入head = [1,2,3,4]
输出head = [2,1,4,3]
2.1递归法
递归的终止条件是链表中没有节点,或者链表中只有一个节点,此时无法进行交换。
用 head 表示原始链表的头节点,新的链表的第二个节点,用 newHead 表示新的链表的头节点,原始链表的第二个节点,则原始链表中的其余节点的头节点是 newHead->next。令 head->next = swapPairs(newHead->next),表示将其余节点进行两两交换,交换后的新的头节点为 head 的下一个节点。然后令 newHead->next = head,即完成了所有节点的交换。最后返回新的链表的头节点 newHead。
//递归法
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode* newhead = head->next;
head->next=swapPairs(newhead->next);
newhead->next=head;
return newhead;
}
2.2迭代法
创建虚拟头结点 dummyHead,令 dummyHead.next = head。令 temp 表示当前到达的节点,初始时 temp = dummyHead。每次需要交换 temp 后面的两个节点。
如果 temp 的后面没有节点或者只有一个节点,则没有更多的节点需要交换,因此结束交换。否则,获得 temp 后面的两个节点 node1 和 node2,通过更新节点的指针关系实现两两交换节点。
总而言之,交换之前的节点关系是 temp -> node1 -> node2,交换之后的节点关系要变成 temp -> node2 -> node1
图示如下:
//迭代法
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode* dummy = new ListNode(-1,head);
ListNode* temp = dummy;
while(temp->next != NULL && temp->next->next != NULL ){
ListNode* node1 = temp->next;
ListNode* node2 = temp->next->next;
temp->next=node2;
node1->next = node2->next;
node2->next = node1;
temp=node1;
}
temp = dummy->next;
delete dummy;
return temp;
}
3.单链表+1
leetcode力扣 369https://leetcode.cn/problems/plus-one-linked-list/description/
4.链表加法
leetcode力扣 445https://leetcode.cn/problems/add-two-numbers-ii/description/
4.1栈
先将两个链表的元素分别压栈,然后再一起出栈,将两个结果分别计算。之后对计算结果取模,模数保存到新的链表中,进位保存到下一轮。完成之后再进行一次反转就行了。
4.2反转链表
使用链表反转,先将两个链表分别反转,最后计算完之后再将结果反转,一共有三次反转操作。
//反转链表
ListNode* reverseList(ListNode* head) {
if(head==NULL) return NULL;
ListNode* cur=head;
ListNode* pre=NULL;
ListNode* next=NULL;
while(cur!=NULL){
next=cur->next;
cur->next=pre;
pre=cur;
cur=next;
}
return pre;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
l1=reverseList(l1);
l2=reverseList(l2);
ListNode* dummy3= new ListNode;
ListNode* cur1=l1;
ListNode* cur2=l2;
ListNode* cur3=dummy3;
int jw=0;
while(cur1!=NULL && cur2!=NULL){
int res=(cur1->val + cur2->val + jw)%10;
cur3->next=new ListNode(res,NULL);
jw=(cur1->val + cur2->val + jw)/10;
cur3=cur3->next;
cur1=cur1->next;
cur2=cur2->next;
}
if(cur1!=NULL && cur2==NULL){
if(jw>0){
while(cur1!=NULL){
if(jw+cur1->val>10){
cur3->next=new ListNode(jw+cur1->val%10,NULL);
cur3=cur3->next;
jw=1;
cur1=cur1->next;
}else if(jw+cur1->val==10){
cur3->next=new ListNode(0,NULL);
cur3=cur3->next;
jw=1;
cur1=cur1->next;
}
else {
cur3->next=new ListNode(jw+cur1->val,NULL);
cur3=cur3->next;
cur1=cur1->next;
jw=0;
}
}
}
cur3->next=cur1;
}
if(cur2!=NULL && cur1==NULL){
if(jw>0){
while(cur2!=NULL){
if(jw+cur2->val>10){
cur3->next=new ListNode(jw+cur2->val%10,NULL);
cur3=cur3->next;
jw=1;
cur2=cur2->next;
}else if(jw+cur2->val==10){
cur3->next=new ListNode(0,NULL);
cur3=cur3->next;
jw=1;
cur2=cur2->next;
}
else {
cur3->next=new ListNode(jw+cur2->val,NULL);
cur3=cur3->next;
cur2=cur2->next;
jw=0;
}
}
}
cur3->next=cur2;
}
if(cur2==NULL && cur1==NULL){
if(jw>0)
cur3->next=new ListNode(jw,NULL);
}
cur3=dummy3->next;
delete dummy3;
cur3=reverseList(cur3);
return cur3;
}