JAVA 反转链表

//头插法
class
Solution { ListNode p1 ; public ListNode reverseList(ListNode head) { ListNode p = head; while(p!=null){ addFirst(p.val); p = p.next; } return p1; } void addFirst(int a){ ListNode renode=new ListNode(a); if(p1!=null){ renode.next = p1; } p1 = renode; } }

 //双指针  par 前面节点

class Solution {
   
    public ListNode reverseList(ListNode head) {          
                ListNode p  =head,par = null;
                while(p!=null){
                    ListNode tmp  = p.next;
                    p.next = par;
                    par = p;
                    p = tmp;
                }
                return par;
    }
   
}
原文地址:https://www.cnblogs.com/tingtin/p/15706255.html