输入一个链表,反转链表后,输出新链表的表头。
-
package test; class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } public class practiceSixteenth { public ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode reversedHead=null; ListNode current=head; ListNode tmp=null; ListNode pre=null; while(current!=null){ tmp=current.next; current.next=pre; if(tmp==null) reversedHead=current; pre=current; current=tmp; } return reversedHead; } }