Pointer rewiring patterns: save the next node before changing links, use a dummy for head edits, and guard nil/null before two-step hops. Traversal is O(n); rewiring at known nodes is O(1).
| Python | Go | TypeScript | C++ |
|---|---|---|---|
class ListNode: |
type ListNode struct { |
class ListNode { |
struct ListNode { |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
O(n) time, O(1) space.def reverse(head): |
O(n), O(1).var prev *ListNode |
O(n), O(1).let prev: ListNode | null = null |
O(n), O(1).ListNode *prev=nullptr, *curr=head; |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
O(a+b) time, O(1) space.def merge(a, b): |
O(a+b), O(1).dummy := &ListNode{} |
O(a+b), O(1).const dummy = new ListNode() |
O(a+b), O(1).ListNode dummy; |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
Middle: O(n), O(1).def middle(head): |
Middle: O(n), O(1).slow, fast := head, head |
Middle: O(n), O(1).let slow = head, fast = head |
Middle: O(n), O(1).auto slow = head; |
Cycle: O(n), O(1).def has_cycle(head): |
Cycle: O(n), O(1).slow, fast := head, head |
Cycle: O(n), O(1).let slow = head, fast = head |
Cycle: O(n), O(1).auto slow = head; |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
O(n) time, O(1) space; assume valid n.def remove_nth_from_end(head, n): |
O(n), O(1).dummy := &ListNode{Next: head} |
O(n), O(1).const dummy = new ListNode(0, head) |
O(n), O(1).ListNode dummy(0, head); |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
O(1) after locating pointers; dst is outside the segment.def splice_after(before, last, dst): |
O(1) after search; dst outside.first := before.Next |
O(1) after search; dst outside.const first = before.next! |
O(1) after search; dst outside.auto first = before->next; |
| Python | Go | TypeScript | C++ |
|---|---|---|---|
|
|
|
|
Docs: docs.python.org · pkg.go.dev · developer.mozilla.org · en.cppreference.com