24. 两两交换链表中的节点
题目描述:

解题代码:
class Solution(object):
def swapPairs(self, head):
dummy_head = ListNode(next=head)
cur = dummy_head
while cur.next != None and cur.next.next != None:
temp = cur.next
temp1 = cur.next.next.next
cur.next = cur.next.next
cur.next.next = temp
temp.next = temp1
cur = cur.next.next
return dummy_head.next
19.删除链表的倒数第N个节点
题目描述:

class Solution(object):
def removeNthfromEnd(self, head, n):
dummy_head = ListNode(next=head)
fast = dummy_head
slow = dummy_head
for _ in range(n):
fast = fast.next
fast = fast.next
while fast != None:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy_head.next
面试题 02.07. 链表相交
题目描述:

解题代码:
class Solution(object):
def getIntersectionNode(self, headA, headB):
A, B = headA, headB
while A != B:
A = A.next if A else headB
B = B.next if B else headA
return A
环形链表
题目描述:

解题代码:
class Solution(object):
def detectCycle(self, head):
fast = head
slow = head
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
index1 = fast
index2 = head
while index1 != index2:
index1 = index1.next
index2 = index2.next
return index2
return None