题目链接:24. 两两交换链表中的节点 - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:帮你把链表细节学清楚! | LeetCode:24. 两两交换链表中的节点_哔哩哔哩_bilibili
思路:
使用虚拟头节点,双指针法,tmp1和tmp2,将移动后会丢失的关键节点先保存下来;
cur指向待交换两节点的前一个结点,注意while循环的边界条件:如果为偶数,cur->next就为NULL,直接终止;如果为奇数,cur->next->next就为NULL,终止。故cur->next != nullptr && cur->next->next != nullptr
// C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* d_head = new ListNode(0);
d_head->next = head;
ListNode* cur = d_head;
while (cur->next != nullptr && cur->next->next != nullptr) {
ListNode* tmp1 = cur->next;
ListNode* tmp2 = cur->next->next->next;
cur->next = cur->next->next;
cur->next->next = tmp1;
cur->next->next->next = tmp2;
cur = cur->next->next;
}
ListNode* res_head = d_head->next;
delete d_head;
return res_head;
}
};
#python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
d_head = ListNode(next=None)
d_head.next = head
cur = d_head
while cur.next != None and cur.next.next != None :
tmp1 = cur.next
tmp2 = cur.next.next.next
cur.next = cur.next.next
cur.next.next = tmp1
cur.next.next.next = tmp2
cur = cur.next.next
return d_head.next
题目链接:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:链表遍历学清楚! | LeetCode:19.删除链表倒数第N个节点_哔哩哔哩_bilibili
思路:
双指针法,秒就秒在:要删除倒数第n个节点,让fast先移动n步,然后让fast和slow同时移动,直到fast指向链表末尾。删掉slow所指向的节点就可以了,然后手动释放;
这里要注意:slow要指向要删除的节点的前一个结点,所以,要n++一下,让fast多走一步,并设置一个头节点;
//C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* d_head = new ListNode(0);
d_head->next = head;
ListNode* fast = d_head;
ListNode* slow = d_head;
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;
ListNode* res_head = d_head->next;
delete d_head;
return res_head;
}
};
#python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
d_head = ListNode(next=head)
fast = d_head
slow = d_head
n += 1
while n != 0 and fast != None :
n -= 1
fast = fast.next
while fast != None :
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return d_head.next
题目链接:142. 环形链表 II - 力扣(LeetCode)
文章讲解:代码随想录 (programmercarl.com)
视频讲解:把环形链表讲清楚! 如何判断环形链表?如何找到环形链表的入口? LeetCode:142.环形链表II_哔哩哔哩_bilibili
思路:
这道题,不仅考察链表,还考察数学,数学推导理解了之后,代码就很容易了
主要考察两知识点:
- 判断链表是否环
- 如果有环,如何找到这个环的入口
判断链表是否有环
使用快慢指针法,分别定义 fast 和 slow 指针,从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果 fast 和 slow指针在途中相遇 ,说明这个链表有环。
注意:相对速度为1,一定能相遇,且相遇在环中。
如果有环,如何找到这个环的入口
从头结点出发一个指针,从相遇节点 也出发一个指针,这两个指针每次只走一个节点, 那么当这两个指针相遇的时候就是 环形入口的节点。
也就是在相遇节点处,定义一个指针index1,在头结点处定一个指针index2。
为什么?详见文章和视频详细讲解
//C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head;
ListNode* slow = head;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode* idx1 = fast; // or ListNode* idx1 = slow;
ListNode* idx2 = head;
while (idx1 != idx2) {
idx1 = idx1->next;
idx2 = idx2->next;
}
return idx1; // idx2
}
}
return NULL;
}
};
#python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
fast = slow = head
while fast and fast.next :
fast = fast.next.next
slow = slow.next
if fast == slow :
idx1 = fast
idx2 = head
while idx1 != idx2 :
idx1 = idx1.next
idx2 = idx2.next
return idx1
return None
思路:
//C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
while (curA != NULL) {
lenA++;
curA = curA->next;
}
while (curB != NULL) {
lenB++;
curB = curB->next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
swap(lenA, lenB);
swap(curA, curB);
}
int gap = lenA - lenB;
while (gap--) {
curA = curA->next;
}
while (curA != NULL) {
while (curA != curB) {
curA = curA->next;
curB = curB->next;
}
return curA;
}
return NULL;
}
};
#python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
lenA = self.getLength(headA)
lenB = self.getLength(headB)
# 通过移动较长的链表,使两链表长度相等
if lenA > lenB:
headA = self.moveForward(headA, lenA - lenB)
else:
headB = self.moveForward(headB, lenB - lenA)
# 将两个头向前移动,直到它们相交
while headA and headB:
if headA == headB:
return headA
headA = headA.next
headB = headB.next
return None
def getLength(self, head):
length = 0
while head:
length += 1
head = head.next
return length
def moveForward(self, head, steps):
while steps > 0:
head = head.next
steps -= 1
return head