链表反转

class list{ node head; int length; } class node{ String info; node next; } 一 先看用迭代(iterative)方式实现代码(JAVA): public void list_reverse() { node pos1, pos2, bufnode; pos1 = head; pos2 = null; while (pos1 != null) { bufnode = pos1; pos1 = pos1.next; bufnode.next = pos2; pos2 = bufnode; } head = pos2; //The final list's head is pos2. } 二 接下来是递归(recursion)代码,比较舒服。: public void list_reverse() { head = node_reverse(head); } private node node_reverse(node N) { if(N == null) { return null; } else{ node head = N; node tail = N.next; head.next = null; return node_concat(node_reverse(tail),head); } } private node node_concat(node N1,node N2) { if(N1 == null) { return N2; } else { N1.next = node_concat(N1.next,N2); return N1; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值