Leetcode reorder-list 链表重排序

题目描述

 

Given a singly linked list LL 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…

You must do this in-place without altering the nodes' values.

For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.

将链表L0→L1→…→Ln-1→Ln 排列成L0→Ln→L1→Ln-1→L2→Ln-2→ 
观察第二个链表是交替排列的。依次从原链表头取一个,从原链表尾取一个。 

问题分析

单链表的最大问题就是不能够逆向获取节点,因此我的思路是将每一个节点的指针保存到vector中,这样能够做到随机访问。

vector就是一个不定长数组。不仅如此,它把一些常用操作“封装”在了vector类型内部。例如,若a是一个vector,可以用a.size()读取它的大小,a.resize()改变大小,a.push_back()向尾部添加元素,a.pop_back()删除最后一个元素。

vector是一个模板类,所以需要用vector<int> a或者vector<double> b这样的方式来声明一个vector。vector<int>是一个类似于int a[]的整数数组,而vector<String>就是一个类似于string a[]的字符串数组。vector看上去像是"一等公民",因为它们可以直接赋值,还可以作为函数的参数或者返回值,而无须像传递数组那样另外用一个变量指定元素个数。

算法思路

1、判断是否为空链表或者单链表

2、while遍历单链表,将每一个结点的指针p保存到vector中 

3、设置两个游标pre和rear,当pre<=rear的时候进行循环每一次访问vector中的元素。
4、新链表的newRead->next指向当前插入的结点,然后将当前结点的next=null,然后向后移动

c++代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#include <vector>
class Solution {
public:
    void reorderList(ListNode *head) {
        if(head == NULL || head -> next == NULL)
            return;
        ListNode *p = head;
        vector<ListNode *>  store;
        while(p != NULL){
            store.push_back(p);
            p = p -> next;
        }
        int m = 0,n = store.size() -1;
        int count = 0;
        ListNode * near = new ListNode(0);
        while(m <= n)
        {
            if(count % 2 == 0)
            {
                near->next = store[m];
                store[m]->next = NULL;
                near = store[m];
                m++;
            } else{
                near -> next = store[n];
                store[n] -> next = NULL;
                near = store[n];
                n--;
            }
            count++;
        } 
           head = NULL;
    }
 
};

Java代码:

思路是将链表分为前后两个部分,然后将后面的一个部分进行逆序,然后依次插入。具体的实现是使用快慢指针找到链表的中点,将链表分为两个部分,然后对第二部分进行逆转,然后依次将第二部分链表插入到第一部分链表。

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        //链表长度小于二直接返回
        if(head == null || head.next == null || head.next.next == null)
            return ;
        //第二段链表逆序后的头部指针
        ListNode head2 = null;
        //用于找到中心节点的快慢指针
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null&& fast.next.next != null)
        {
            slow = slow.next;
            fast = fast.next.next;
        }
        head2 = reverse(slow.next);
        //注意置空
        slow.next = null;
        //合并两个链表
        while(head2!=null){
            ListNode next = head.next;
            head.next = head2;
            head2 = head2.next;
            head = head.next;
            head.next = next;
            head = next;
        }    
    }

    public static ListNode reverse(ListNode head)
    {
        if(head == null || head.next == null)
            return head;
        ListNode cur = head;
        ListNode p = head.next;
        cur.next = null;
//cur为当前翻转链表的头部结点, p为下一个要翻转的结点
        while(p != null)
        {
//需要使用tmp来保存当前翻转结点的下一个结点
           ListNode tmp  = p.next;
            p.next = cur;
            cur = p;
            p = tmp;
        }
        return cur;
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值