刷题的第四天,希望自己能够不断坚持下去,迎来蜕变。😀😀😀
刷题语言:C++ / Python
Day4 任务
● 24. 两两交换链表中的节点
● 19.删除链表的倒数第N个节点
● 面试题 02.07. 链表相交
● 142.环形链表II
1 两两交换链表中的节点
用虚拟头结点
伪代码:
dummyhead->next = head; // 设置一个虚拟头结点
cur = dummyhead;// 将虚拟头结点指向head
while (cur->next != NULL && cur->next->next != NULL) // 注意不能颠倒顺序,空指针不能访问->next
{tmp1 = cur->next; // 记录临时节点tmp2 = cur->next->next->next;// 记录临时节点cur->next = cur->next->next; // 步骤一cur->next->next = tmp1; // 步骤二tmp1->next = tmp2; // 步骤三cur = cur->next->next; // cur移动两位
}
return dummyhead->next;
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)
C++:
class Solution {
public:ListNode* swapPairs(ListNode* head) {ListNode* dummyhead = new ListNode(0);dummyhead->next = head;ListNode* cur = dummyhead;while (cur->next != NULL && cur->next->next != NULL){ListNode* tmp1 = cur->next;ListNode* tmp2 = cur->next->next->next;cur->next = cur->next->next;cur->next->next = tmp1;tmp1->next = tmp2;cur = cur->next->next;}head = dummyhead->next;delete dummyhead;return dummyhead->next;}
};
Python:
class Solution(object):def swapPairs(self, head):""":type head: ListNode:rtype: ListNode"""dummyhead = ListNode(next=head)cur = dummyheadwhile cur.next != None and cur.next.next != None:tmp1 = cur.nexttmp2 = cur.next.next.nextcur.next = cur.next.nextcur.next.next = tmp1tmp1.next = tmp2cur = cur.next.nextreturn dummyhead.next
2 删除链表的倒数第N个节点
思路:
要删除倒数第n个节点,让fast移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点,链表的删除操作应该是要让删的前一个节点指向要删节点的后一个节点,所以应该先让fast移动n+1步,这样slow慢指针最后就是指向要删节点的前一个节点
伪代码:
fast = dummyhead;
slow = dummyhead;
n++;
while (n-- && fast != NULL)
{fast = fast->next;
}
while (fast!= NULL)
{fast = fast->next;slow = slow->next;
}
slow->next = slow->next->next;
return dummyhead->next;
C++:
class Solution {
public:ListNode* removeNthFromEnd(ListNode* head, int n) {ListNode* dummyhead = new ListNode(0);dummyhead->next = head;ListNode* fast = dummyhead;ListNode* slow = dummyhead;n++;while (n-- && fast != NULL){fast = fast->next;}while (fast != NULL){fast = fast->next;slow = slow->next;}ListNode* tmp = slow->next;slow->next = slow->next->next;delete tmp;return dummyhead->next;}
};
Python:
class Solution(object):def removeNthFromEnd(self, head, n):""":type head: ListNode:type n: int:rtype: ListNode"""dummyhead = ListNode(next=head)fast = dummyheadslow = dummyheadn += 1while n and fast != None:n -= 1fast = fast.nextwhile fast != None:slow = slow.nextfast = fast.nextslow.next = slow.next.nextreturn dummyhead.next
3 链表相交
思路:
求两个链表交点的指针交点不是数值相等,是指针相等
求出两个链表的长度,并求出两个链表长度的差值,让curA移动到和curB 末尾对齐的位置
此时比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点,否则循环退出返回空指针。
伪代码:
(1)curA = headA; curB = headB;
(2)求A,B链表的长度
(3)curA = headA; curB = headB;
(4)保证A长度是最大的
(5)让curA移动到和curB 末尾对齐的位置
(6)比较curA和curB是否相同,如果不相同,同时向后移动curA和curB,如果遇到curA == curB,则找到交点。否则循环退出返回空指针
C++:
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {ListNode* curA = headA;ListNode* curB = headB;int lenA = 0;int lenB = 0;while (curA != NULL)// 求链表A的长度{lenA++;curA = curA->next;}while (curB != NULL)// 求链表B的长度{lenB++;curB = curB->next;}curA = headA;curB = headB;if (lenA < lenB)// 让curA为最长链表的头,lenA为其长度{swap(lenA, lenB);swap(curA, curB);}int gap = lenA - lenB;// 求长度差// 让curA和curB在同一起点上(末尾位置对齐)while(gap--){curA = curA->next;}while (curA != NULL){ // 遍历curA 和 curB,遇到相同则直接返回if (curA == curB){return curA;}curA = curA->next;curB = curB->next;}return NULL;}
};
Python:
class Solution(object):def getIntersectionNode(self, headA, headB):""":type head1, head1: ListNode:rtype: ListNode"""curA = headAcurB = headBlenA = 0lenB = 0while curA != None:lenA += 1curA = curA.nextwhile curB != None:lenB += 1curB = curB.nextcurA = headAcurB = headBif lenA < lenB:lenA, lenB = lenB, lenAcurA, curB = curB, curAgap = lenA - lenBfor i in range(gap):curA = curA.nextwhile curA != None:if curA == curB:return curAcurA = curA.nextcurB = curB.nextreturn None
4 环形链表II
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
思路:
(1)判断链表是否有环
(2)如果有环,如何找到这个环的入口
如何判断链表有环
使用快慢指针法,定义fast和slow指针,从头节点出发,快指针每次移动两个节点,慢指针每次移动一个节点,如果fast和slow指针在途中相遇,说明这个链表有环
如果有环,如何找到这个环的入口
相遇时,slow指针走过的节点数: x + y x + y x+y
fast指针走过的节点数: x + y + n ( y + z ) x + y + n(y + z) x+y+n(y+z)
( x + y ) ∗ 2 = x + y + n ( y + z ) (x+y)*2=x + y + n(y + z) (x+y)∗2=x+y+n(y+z)
x = n ( y + z ) − y x=n(y+z)-y x=n(y+z)−y
找环形的入口就是求x,x表示头结点到环形入口节点的的距离。
x = ( n − 1 ) ( y + z ) + z x=(n-1)(y+z)+z x=(n−1)(y+z)+z注意这里n一定是大于等于1的,因为 fast指针至少要多走一圈才能相遇slow指针
当 n为1的时候,公式就化解为 x = z x = z x=z
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。让index1和index2同时移动,每次移动一个节点, 那么他们相遇的地方就是 环形入口的节点。
伪代码:
(1)fast和slow指针指向头节点
(2)循环:当fast和fast->next等于NULL循环终止。slow走一步,fast走两步
(3)当两指针相遇,index1 = fast,index2 = head。两index同时向后移如果相等,返回环的入口
(4)找不到返回NULL
C++:
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode* fast = head; // fast和slow指针指向头节点ListNode* slow = head;while (fast != NULL && fast->next != NULL) // 循环:当fast和fast->next等于NULL循环终止{slow = slow->next; // slow走一步fast = fast->next->next;// fast走两步if (slow == fast) // 当两指针相遇,index1 = fast,index2 = head。两index同时向后移如果相等,返回环的入口{ListNode* index1 = fast;ListNode* index2 = head;while (index1 != index2){index1 = index1->next;index2 = index2->next;}return index1;}}return NULL;}
};
Python:
class Solution(object):def detectCycle(self, head):""":type head: ListNode:rtype: ListNode"""fast = headslow = headwhile fast != None and fast.next != None:slow = slow.nextfast = fast.next.nextif slow == fast:index1 = fastindex2 = headwhile index1 != index2:index1 = index1.nextindex2 = index2.nextreturn index2return None
鼓励坚持四天的自己😀😀😀