r/csinterviewproblems • u/parlezmoose • Dec 17 '15
[Linked Lists] Reverse a singly linked linked list in place
Reverse a singly linked list in place, returning the new head node. You may assume there are no cycles. You may assume you are always passed an instance of a "Node" class that contains a reference to the next node.
Sample input: 1 -> 2 -> 3 -> 4
Sample output: 4 -> 3 -> 2 -> 1
6
Upvotes
2
u/parlezmoose Dec 17 '15 edited Dec 17 '15
1
u/WarDEagle Dec 22 '15
Java
In place, but I'm having trouble coming up with big-o unless I actually calculate T(n). Essentially, the outer while loop executes n/2 times, and the inner loop executes n-2, -2, -2, etc. Technically O(n*(n/2)) would cover it because that is an upper bound of the worst case, but I'd like to dial it in if anyone has any input.
1
u/sir_codes_alot Dec 31 '15
Simple solution: