//实现链表翻转,头节点不动公共void reverse (stunodehead ) stunodereversehead=newstunode ) 0、'、'''); //反转节点stuNode cur=head.next; //表示当前节点stuNode curNext; //判断是否为当前节点的下一个节点//空列表if (head.next==null ) {return; }while(cur!=null}{curnext=cur.next; cur.next=reverseHead.next; reverseHead.next=cur; cur=curNext; head.next=reverseHead.next; }测试
publicclassstulinklistdemo { publicstaticvoidmain (字符串[ ] args ) ) stunodestu1=newstunode(1,' lgdls ','在线) stunodestu2=newstunode(2,'孝顺的向日葵','离线'); stunodestu3=newstunode(3,'自由的凉面','在线'); stunodestu4=newstunode(4,' yxdjb ','在线'); stulinkliststulist=newstulinklist (; stulist.addbyorder(stu1; stulist.addbyorder(stu3; stulist.addbyorder(stu4; stulist.addbyorder(stu2; stulist.list (; //测试反转System.out.println ('反转后); stu list.reverse (stu list.gethead ); stulist.list (; }结果如下实现过程中出现的问题
1.return和break的使用方法: return既可以用于返回,也可以用于退出。 break用于退出循环,可以使用带标签的break退出外部循环
2 .关于new对象和赋值null的区别
stunodereversehead=newstunode (0,',' '; new对象将reverseHead指向stuNode在内存中创建的地址
stuNode reverseHead=null时; 不会占用存储器区域
什么时候用new对象,什么时候用null
需要new才能添加取值操作
对于辅助变量,使用空值进行遍历等