Leetcode24. 两两交换链表中的节点
题目链接:24. 两两交换链表中的节点
C++:
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *dummyhead = new ListNode(0, head);
ListNode *cur = dummyhead;
while(cur->next != nullptr && cur->next->next != nullptr)
{
ListNode *temp = cur->next;
cur->next = temp->next;
temp->next = cur->next->next;
cur->next->next = temp;
cur = temp;
}
return dummyhead->next;
}
};
Python:
class Solution:
def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummyhead = ListNode(0, head)
cur = dummyhead
while cur.next != None and cur.next.next != None:
temp = cur.next
cur.next = temp.next
temp.next = cur.next.next
cur.next.next = temp
cur = temp
return dummyhead.next
Leetcode19.删除链表的倒数第N个节点
题目链接:19.删除链表的倒数第N个节点
C++:(双指针)
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummyhead = new ListNode(0, head);
ListNode *fast = dummyhead;
ListNode *slow = dummyhead;
for(int i=0; i < n; i++)
{
fast = fast->next;
}
while(fast->next != nullptr)
{
fast = fast->next;
slow = slow->next;
}
slow->next = slow->next->next;
return dummyhead->next;
}
};
Python:
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy_head = ListNode(0, head)
fast = dummy_head
slow = dummy_head
while(fast and n):
fast = fast.next
n -= 1
while(fast.next):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy_head.next
面试题 02.07. 链表相交
题目链接:面试题 02.07. 链表相交
C++:(暴力解法)
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// ListNode *curA = headA;
ListNode *curB = headB;
ListNode *curA = headA;
// if(curA == NULL || curB == NULL)
// return NULL;
while(curB != NULL)
{
ListNode *curA = headA;
while(curA != NULL)
{
if(curA != curB)
curA = curA->next;
else
break;
}
if(curA != curB)
curB = curB->next;
else
return curA;
}
return NULL;
}
};
Python:(求长度)
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
lenA, lenB = 0, 0
cur = headA
while cur:
cur = cur.next
lenA += 1
cur =headB
while cur:
cur = cur.next
lenB += 1
if lenA > lenB: # 使B成为长链表
lenA, lenB = lenB, lenA
headA, headB = headB, headA
curA, curB = headA, headB
for _ in range(lenB - lenA):
curB = curB.next
while curA:
if(curA == curB):
return curA
else:
curA = curA.next
curB = curB.next
return None
Leetcode142.环形链表II
题目链接:142.环形链表II
注意:
慢指针行走距离:x+y
快指针行走距离:x+y+n*(y+z)
即:2*(x+y) = x+y+n*(y+z)
x+y=n*(y+z)
x= n*(y+z)-y
x= (n-1)*(y+z)+z
C++:
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while(fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
if(fast == slow)
{
ListNode *index1 = slow;
ListNode *index2 = head;
while(index1 != index2)
{
index1 = index1->next;
index2 = index2->next;
}
return index2;
}
}
return NULL;
}
};
Python:
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast, slow = head, head
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
index1 = slow
index2 = head
while index1 != index2:
index1 = index1.next
index2 = index2.next
return index2
return None