题目描述
输入一个链表,反转链表后,输出链表的所有元素。
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 }