classSolution { public: ListNode *detectCycle(ListNode *head){ unordered_set<ListNode*> hashTable; while (head != nullptr) { if (hashTable.count(head)) return head; hashTable.insert(head); head = head->next; } returnnullptr; } };
1 2 3 4 5 6 7 8 9 10 11 12
publicclassSolution { public ListNode detectCycle(ListNode head) { Set<ListNode> hashTable = newHashSet<ListNode>(); while (head != null) { if (hashTable.contains(head)) return head; hashTable.add(head); head = head.next; } returnnull; } }
1 2 3 4 5 6 7 8 9
classSolution: defdetectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: hashTable = set() while head: if head in hashTable: return head hashTable.add(head) head = head.next returnNone
publicclassSolution { public ListNode detectCycle(ListNode head) { if (head == null || head.next == null) returnnull; ListNodefastNode= head; ListNodeslowNode= head; while (fastNode != null) { if (fastNode == null || fastNode.next == null) returnnull; slowNode = slowNode.next; fastNode = fastNode.next.next; if (fastNode == slowNode) { ListNodecur= head; while ( cur != slowNode) { cur = cur.next; slowNode = slowNode.next; } return cur; } } returnnull; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defdetectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: if (head == Noneor head.next == None): returnNone fastNode = head slowNode = head while (fastNode != None): if (fastNode == Noneor fastNode.next == None): returnNone slowNode = slowNode.next fastNode = fastNode.next.next if (fastNode == slowNode): cur = head while ( cur != slowNode): cur = cur.next slowNode = slowNode.next return cur returnNone
classSolution { public ListNode removeNthFromEnd(ListNode head, int n) { intlength= GetLength(head); ListNodecur=newListNode(0, head); ListNodedummy= cur; for (inti=1; i < length - n + 1; ++i) { cur = cur.next; } cur.next = cur.next.next; ListNodeans= dummy.next; return ans; }
publicintGetLength(ListNode head) { intlength=0; while (head != null) { head = head.next; ++length; } return length; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defremoveNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: defGetLength(head : Optional[ListNode]) -> Optional[int]: length = 0 while (head != None): head = head.next length += 1 return length length = GetLength(head) cur = ListNode(0, head) dummy = cur for i inrange(1, length + 1 -n): cur = cur.next cur.next = cur.next.next ans = dummy.next return ans
2. 栈
用一个栈去存储节点,之后弹出的第n个节点就是需要删除的地方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: ListNode* removeNthFromEnd(ListNode* head, int n){ ListNode* cur = newListNode(0, head); ListNode* dummy = cur; stack<ListNode*> stk; while (cur != nullptr) { stk.push(cur); cur = cur->next; } for (int i = 0; i < n; ++i) stk.pop(); head = stk.top(); head->next = head->next->next; return dummy->next; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNodecur=newListNode(0, head); ListNodedummy= cur; Deque<ListNode> stk = newLinkedList<>(); while (cur != null) { stk.push(cur); cur = cur.next; } for (inti=0; i < n; ++i) stk.pop(); head = stk.peek(); head.next = head.next.next; return dummy.next; } }
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defremoveNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: cur = ListNode(0, head) dummy = cur stk = list() while (cur != None): stk.append(cur) cur = cur.next for i inrange(n): stk.pop() head = stk[-1] head.next = head.next.next return dummy.next
3. 双指针
使用两个指针,快指针比慢指针多n个节点,快指针到达末尾则慢指针位置就是需要处理的地方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: ListNode* removeNthFromEnd(ListNode* head, int n){ ListNode* pre = newListNode(0, head); ListNode* slow = pre; // 不从head开始的原因在于,可以刚好到达倒数第n节点的前继结点 ListNode* fast = head; for (int i = 0; i < n; ++i) fast = fast->next; while (fast != nullptr) { slow = slow->next; fast = fast->next; } slow->next = slow->next->next; return pre->next; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNodepre=newListNode(0, head); ListNodeslow= pre; ListNodefast= head; for (inti=0; i < n; ++i) fast = fast.next; while (fast != null) { slow = slow.next; fast = fast.next; } slow.next = slow.next.next; return pre.next; } }
1 2 3 4 5 6 7 8 9 10 11
classSolution: defremoveNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: pre = ListNode(0, head) slow = pre fast = head for i inrange(n): fast = fast.next while (fast != None): slow = slow.next fast = fast.next slow.next = slow.next.next return pre.next