public static Node reverseNode(Node node){
Node newHead=null;
while(node!=null){
Node temp=node;
node.setNext(newHead);
newHead=node;
if(node.getNext()!=null){
node=temp.getNext();
}else{
node=null;
}
}
return newHead;
}
if node==null is true, then return null;
if node!=null is true, then enter the loop:
if node.getNext()!=null is true, then the node=node.getNext() after one loop.
if node.getNext()==null is ture, then the code node.setNext(newHead) will make node.getNext()!=null, but the code with if-else will make node null.
if node.getNext()==null is ture, newHead is null, the code without if-else will also make node null.
if-else语句可以换成
node=temp.getNext();
if-else 的作用是node=node.getNext(); 而进入循环前已经判定node不为null.