5,323
edits
Line 1: | Line 1: | ||
Insights from Leetcode problems. | Insights from Leetcode problems. | ||
== | ==Two Pointers Algorithms== | ||
Having two pointers can make your algorithm run in linear time. | |||
====Finding a cycle in a linked-list==== | ====Finding a cycle in a linked-list==== | ||
Use two runners. <math>O(n)</math><br> | Use two runners. <math>O(n)</math><br> | ||
Line 7: | Line 9: | ||
Runner 2 goes one step per iteration.<br> | Runner 2 goes one step per iteration.<br> | ||
If there is a cycle, runner 2 will lap runner 1 within 2 cycles. | If there is a cycle, runner 2 will lap runner 1 within 2 cycles. | ||
==Backtracking== | ==Backtracking== |