python指针直接用.next就可以指向下一个,如在经典的快慢指针中slow与fast都是链表,用.next直接就能将指针指向下一项
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
slow = head
fast = head
# Use two pointers, one moving one step at a time and the other moving two steps at a time.
# When the fast pointer reaches the end, the slow pointer will be at the middle.
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow