Can I patent something I invented at my previous job or at university?
Only if your previous employer or university does not own it, and that depends on what you signed and when you conceived it. Check three things. First, the invention assignment clause in your old employment agreement: most assign inventions made during employment that relate to the employer's business or used its resources, and some include a trailing clause claiming inventions conceived within six or twelve months after leaving; trailing clauses are enforceable in some jurisdictions and not others (California, for example, limits them). Second, conception date: if you conceived the invention after leaving and without using confidential information from the old job, it is generally yours, but you will need to prove the date, so document it. Third, the statutory rules: in the UK, section 39 of the Patents Act 1977 gives the employer only inventions made in the course of your duties; in Germany, service inventions belong to the employer once claimed; in the US the contract governs almost everything.
Universities are a separate case. Most US, UK and German universities claim inventions made by staff, and often by funded students, under their IP policies, and US inventions made with federal funding fall under the Bayh-Dole Act, which gives the university the right to take title. The usual route for a spinout is a licence or assignment from the technology transfer office in exchange for equity or royalties, negotiated before the company files anything. Sweden is the notable exception: under the teacher's exemption, university teachers and researchers own their own inventions, which is why Swedish spinouts often file directly. Whatever the situation, do not file in the company's name and hope: a patent later found to belong to someone else is a due diligence failure that investors treat as fatal. Get the release, licence or written confirmation first, then file.