Reorder List

本文介绍了一种将链表从L0→L1→...→Ln-1→Ln重排序为L0→Ln→L1→Ln-1→L2→Ln-2→...的方法。通过寻找中点、反转后半部分链表并交替合并前半和后半部分来实现。提供了Java和Python两种语言的实现。

Given a singly linked list L: L0 → L1 → … → Ln-1 → Ln

reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

链表的题目非常容易出错,要仔细些。尤其要清楚其中的指针是怎么变化的

java

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */


public class Solution {
    /*
     * @param head: The head of linked list.
     * @return: nothing
     */
    public void reorderList(ListNode head) {
        // write your code here
        if (head == null || head.next == null) {
            return;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        ListNode mid = null;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        mid = slow;
        ListNode tail = reverse(mid.next);
        mid.next = null;
        merge(head, tail);
    }
    private ListNode reverse(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode curr = head;
        ListNode prev = null;
        while (curr != null) {
            ListNode temp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = temp;
        }
        return prev;
    }
    private void merge(ListNode head, ListNode tail) {
        if (head == null || tail == null) {
            return;
        }
        int index = 0;
        ListNode dummy = new ListNode(0);
        while (head != null && tail != null) {
            dummy.next = head;
            head = head.next;
            dummy = dummy.next;
            dummy.next = tail;
            tail = tail.next;
            dummy = dummy.next;
        }
        if (head != null) {
            dummy.next = head;
        } else {
            dummy.next = tail;
        }
    }
}


python

"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""


class Solution:
    """
    @param: head: The head of linked list.
    @return: nothing
    """
    def reorderList(self, head):
        # write your code here
        if head is None or head.next is None:
            return
        mid = self.findMid(head)
        tail = self.reverse(mid.next)
        mid.next = None
        self.merge(head, tail)
        
    def findMid(self, head):
        slow = head
        fast = head.next
        while fast is not None and fast.next is not None:
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def reverse(self, head):
        if head is None:
            return head
        curr = head
        prev = None
        while curr is not None:
            temp = curr.next
            curr.next = prev
            prev = curr
            curr = temp
        return prev
        
    def merge(self, head, tail):
        if head is None or tail is None:
            return
        flag = True
        dummy = ListNode(-1)
        while head is not None and tail is not None:
            if flag:
                dummy.next = head
                head = head.next
            else:
                dummy.next = tail
                tail = tail.next
            flag = not flag
            dummy = dummy.next
        if head is not None:
            dummy.next = head
        else:
            dummy.next = tail
        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值