2024年1月18日发(作者:)

# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# = val# = nextclass Solution(object): def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ cur,pre= head,None while cur!=None: , cur, pre= pre, , cur #同时 return pre5.两两交换链表中的节点

# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# = val# = nextclass Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ NewHead=ListNode(0,head) pre,cur= NewHead, while cur and : #不为空 cur=

, pre = cur, #步骤1 , cur = pre, #步骤2 = cur #步骤3 return 6.删除链表的倒数第N个节点

# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# = val# = nextclass Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ Nhead= ListNode(0,head) slow =fast = Nhead while : n-= 1 fast= if n<0:slow= = #找到开始删除 return 7.链表相交

9.总结快指针,慢指针,指针满天飞。。。。