随笔-14  评论-142  文章-0  trackbacks-0

这是面试的时候,做错的一道题, 回来一上机就写出来了!太丢人了...

 1package ch01;
 2
 3public class Node
 4{
 5    
 6    int data;
 7    
 8    Node next = null;
 9    
10    
11    
12    public Node(int data){
13        this.data = data;
14        
15    }

16    
17    public static Node reverse(Node head){
18        
19        Node p = null;
20        Node q = head;
21        
22        
23        while(head.next != null){
24            
25            p = head.next;
26            head.next = p.next;
27            p.next = q;
28            q = p;
29            
30            
31        }

32        
33        return q;
34    }

35    
36    
37    public static void main(String[] args){
38        Node head = new Node(0);
39        Node tail = head;
40        
41        
42        for(int i = 1 ; i < 10++i){
43            Node p = new Node(i);
44            tail.next = p;
45            tail = p;
46            
47        }

48        
49        head = reverse(head);
50        while(head.next != null){
51            
52            System.out.println(head.data);
53            head = head.next;
54            
55            
56        }

57    }

58    
59
60}

61
posted on 2007-09-05 17:04 liulang 阅读(6473) 评论(5)  编辑  收藏

评论:
# re: java单链表逆序算法 2007-09-06 17:08 | astamei
这段代码好像有问题的吧


以后贴代码的时候请不要加上行号
  回复  更多评论
  
# re: java单链表逆序算法 2007-09-06 17:34 | liulang
他的原题是这样的:

class Node{

int data;
Node next= null;

Node reverse(Node node){

......//让你补充此方法,实现链表逆序

}

}

题目就那么简单,我一开始很纳闷:
1, Node是一个结点,并不是一个链表LinkList类,在Node类中放reverse方法
不符合面向对象设计思想。
2, 传进的参数node,是什么?返回的node又是什么?
后来回来想了半天,才知道传进方法的参数node,应该是链表的头head,返回的是逆序前,最后的结点。
  回复  更多评论
  
# re: java单链表逆序算法 2007-09-07 08:27 | astamei
出题的人还真怪。
呵呵 ~
  回复  更多评论
  
# re: java单链表逆序算法[未登录] 2007-09-09 16:05 | Ryan
这家公司太烂了,不要去,出的题目也这么烂!
  回复  更多评论
  
# re: java单链表逆序算法 2007-09-10 18:37 | suntao19830709@gmail.com
我看的无语  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: