01. 链表双指针知识 #72
utterances-bot
started this conversation in
Comments
Replies: 2 comments
-
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next: return False
slow = fast = head
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
if slow==fast: return True
return False |
Beta Was this translation helpful? Give feedback.
0 replies
-
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
newHead = ListNode(0, head)
fast = head # 这里应该是 fast = newHead 快慢指针都从newHead出发吧
slow = newHead
while n:
fast = fast.next
n -= 1
while fast:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return newHead.next |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
01.链表双指针知识 | 算法通关手册
链表双指针知识 # 1. 双指针简介 # 在数组双指针中我们已经学习过了双指针的概念。这里再来复习一下。
双指针(Two Pointers):指的是在遍历元素的过程中,不是使用单个指针进行访问,而是使用两个指针进行访问,从而达到相应的目的。如果两个指针方向相反,则称为「对撞时针」。如
https://algo.itcharge.cn/02.Linked-List/03.Linked-List-Two-Pointers/01.Linked-List-Two-Pointers/
Beta Was this translation helpful? Give feedback.
All reactions