Nagibator & Memo
Yo Memo, just smashed my own record on a tough LeetCode marathon—got 5 stars in 30 minutes. Think you can out-code me? Let's see if your precision can keep up with my speed.
Nice record, that’s impressive. I’m more about clean, bug‑free code than raw speed, but I’m up for a friendly showdown—send me a problem and let’s see who can hit that 5‑star mark first.
Alright, here’s a classic: Reverse a singly linked list in place and return the new head. 5-star, no extra space, no dummy nodes. Who’s gonna finish first? Let the battle begin!
Sure thing, here’s the classic in‑place reversal, no dummy nodes, no extra space, just three pointers: prev, curr, next. The idea is to iterate through the list, rewire each node’s next to point back to its predecessor, and finally return the last node processed as the new head.
```
Node reverseLinkedList(Node head) {
Node prev = null
Node curr = head
while curr != null {
Node next = curr.next // remember the next node
curr.next = prev // reverse the link
prev = curr // move prev forward
curr = next // move curr forward
}
return prev // new head of the reversed list
}
```
That’s it—O(n) time, O(1) space. Let me know how you’d optimize or if you’re looking for a recursive version. Good luck!
Nice clean code, but I’d give you a twist: handle null or single-node lists in a single line and throw in a tail-call optimization. Think you can beat that? Let’s see what you’ve got.
Node reverse(Node head) { return (head==null||head.next==null)?head:reverseTail(head,null); } Node reverseTail(Node curr, Node prev) { return curr==null?prev:reverseTail(curr.next,curr); }
Nice recursion, but you just gave me a 5‑star cheat sheet—no speed, no extra space, no dummy nodes, just pure elegance. I’ll keep that in mind for next time. Ready to see who actually crushes a leaderboard? Let's do this.