LeetCode-026. 重排链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        ListNode mid = middleNode(head);
        ListNode tail=reverseList(mid.next);
        ListNode curr = head;
        while (tail!=null&&curr!=null){
            ListNode next = curr.next;
            ListNode tailNext = tail.next;
            curr.next=tail;
            curr.next.next=next;
            tail=tailNext;
            curr=next;
        }
        curr.next=tail;
    }
    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }
}

2、利用线性表存储该链表,然后利用线性表可以下标访问的特点,直接按顺序访问指定元素,重建该链表即可。 

class Solution {
    public void reorderList(ListNode head) {
        if (head == null) {
            return;
        }
        List<ListNode> list = new ArrayList<ListNode>();
        ListNode node = head;
        while (node != null) {
            list.add(node);
            node = node.next;
        }
        int i = 0, j = list.size() - 1;
        while (i < j) {
            list.get(i).next = list.get(j);
            i++;
            if (i == j) {
                break;
            }
            list.get(j).next = list.get(i);
            j--;
        }
        list.get(i).next = null;
    }
}

 

class Solution {
    public void reorderList(ListNode head) {
        ListNode left=new ListNode(0,head);
        ListNode right=new ListNode(0,head);
        DFS(left,right);//递归

    }

    public ListNode DFS(ListNode left,ListNode right){
        if(right==null){
            return left;//left指向为止末尾
        }
        if(right.next==null){
            return left.next;//left指向为止末尾
        }
        //快慢指针,right到了末尾,返回node就是未拼接的最后一个节点
        ListNode node=DFS(left.next,right.next.next);
        //2->3->4   ===    4->2->3
        if(node.next==null){
            return null;//为空
        }
        ListNode temp=left.next;
        left.next=node.next;
        node.next=node.next.next;
        left.next.next=temp;//交换


        return node;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值