c*******r 发帖数: 309 | 1 问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,如果单数
情况返回最后一个, 要求interative和recursive.
实在不会写. 在java里用linkedlist的libirary, node怎么表示啊?
有人能否上个java的代码啊......
linkedlist把我搞晕了 |
l******n 发帖数: 492 | 2 把java 里的object看成指针
【在 c*******r 的大作中提到】 : 问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,如果单数 : 情况返回最后一个, 要求interative和recursive. : 实在不会写. 在java里用linkedlist的libirary, node怎么表示啊? : 有人能否上个java的代码啊...... : linkedlist把我搞晕了
|
c*******r 发帖数: 309 | |
n*******w 发帖数: 687 | 4 java的linkedlist把node封装起来了。
直接提供类似array的api。用api太简单了,有点cheat,而且效率不高。
自己写node class,跟c应该差不多。 |
y*******g 发帖数: 6599 | 5 class Node{
Object value;
Node next;
}
【在 c*******r 的大作中提到】 : 可是object也没有.next啊
|
b******g 发帖数: 1721 | 6 这种只是体力劳动的,我最擅长了。 recursive 我就不写了,给一个iterative吧。
public class myLinkedList{
myLinkedList next;
int value;
myLinkedList stwichIterative(){
if(mls==null)
return mls;
if(mls.size()%2==1)
return last();
//begin switch
myLinkedList first=this;
myLinkedList third=null;
if(this.next!=null && this.next.next!=null)
third=this.next.next;
else
return this;
while(true){
int tmp=third.value;
third.value=first.value;
first.value=tmp;
if(third.next!=null
&&third.next.next.next!=null){
first=third.next;
third=third.next.next;
}else
break;
}
return this;
}
myLinkedList last(){
if(this.next==null) return this;
return this.next.last();
}
int size(){
if(this.next==null) return 1;
return next.size()+1;
}
}
如果有包子就更好了,没有无所谓的。 |
B*******1 发帖数: 2454 | 7 2个很容易,写一个general的可以处理反转k个的,2的情况直接设置k=2就好了。 |