LeetCode:144 重排链表(反转+快慢指针)

class Solution {


    public ListNode Find(ListNode head){
        ListNode slow = head;
        ListNode quick = head;
        while(slow!=null&&quick!=null){
            if(quick.next!=null)
                quick=quick.next.next;
            else{
                break;
            }
             slow=slow.next;
        }
        ListNode a = slow.next;
        slow.next = null;
        return a;
    }

    public void Reverse(ListNode t,ListNode d){
        while(d!=null){
            ListNode a = d;
            d = d.next;
            a.next = t.next;
            t.next = a;
            //d = d.next;
        }
        
        return;
    }

    public void reorderList(ListNode head) {

        if(head==null){
            return;
        }

        ListNode mid = Find(head);
        ListNode t = new ListNode(0,null);
        Reverse(t,mid);
        ListNode res = head;
        t=t.next;
       
        while(res!=null&&t!=null){
            ListNode tmp = t;
            t=t.next;
            tmp.next=res.next;
            res.next = tmp;
            if(res.next!=null)
                res = res.next.next;
            else{
                return;
            }
        }
    }
}

 

posted @ 2020-10-05 09:50  dlooooo  阅读(76)  评论(0编辑  收藏  举报