粽子丫丫

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

题目描述

输入一个链表,反转链表后,输出链表的所有元素。
 
tip:用两个指针
 
 
 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5  
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 //很奇怪,一直显示通过33.3%的测试用例,但是用别人同样的代码,就显示通过,这是什么原因
11 public class Solution {
12     public ListNode ReverseList(ListNode head) {
13         ListNode pre = null;
14         ListNode next = null;
15         while (head != null) {
16             next = head.next;
17             head.next = pre;
18             pre = head;
19             head = next;
20         }
21         return pre;
22     }
23 }

 

posted on 2017-04-11 13:21  粽子丫丫  阅读(136)  评论(0编辑  收藏  举报