First off, I'm assuming P1040 is actually P0140. The FSM doesn't make mention of P1040 anywhere, but P0140 is directly related to two of your other codes. Second, I'm not sure what you mean by red vs. white. I assumed the color of the unit you used to pull the codes?
P0138, P0139, and P0140 are all due to your Heated oxygen sensor 2 (Bank 1).
P0158 and P0159 are due to your Heated oxygen sensor 2 (Bank 2).
P0505 is the Idle Air Control Valve.
Stalling unless you step on the gas pedal after starting definitely sounds like an IACV problem. Since you've replaced the unit twice I'm suspecting a harness problem. There are some troubleshooting steps for this in the FSM at EC-426 in the 2001 Pathfinder/QX4 manual.
As for the oxygen sensors, I believe the ones that are having problems with are the ones after the catalytic converter (downstream). They're right in front of your face when you crawl under the car and look at the exhaust. Be warned: running the wires up to where they need to be plugged in is a bit challenging when you replace them. Also, I had some problems with the connectors being different when I replaced mine. That was resolved by splicing the old connectors onto the new wire.
The Bosch part finder will give you the model numbers of the sensors you need:
http://www.boschautoparts.com/vehiclepa ... inder.aspx
Let us know how it goes!