motor limit switches

I have several things connected to my mega and I am trying to minimize the amount of pins used. Is it possible to wire 2 limit switches to control a motor and only use 1 pin on the mega? I know I can wire the switches in parallel, but not sure how to program it.

Thanks for the help.

Sure - wire them so that either one closing connects the pin to Gnd.
Set the pin using to input with internal pullup resistor:
pinMode (pinX, INPUT_PULLUP);

then check for the pin being low

if (digitalRead(pinX) == LOW){
// limit switch is closed, do something
}

most micro style switch's have a normally open, normally closed and common terminal. If yours has this option then as a safety wire the normally closed in series. program as a pullup then examine if high (That way if a wire breaks it fails)

If you only have normally open contacts wire in parallel the program as a pullup and examine if low.