[LeetCode] Reorder List

1.Description

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-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}.


2.Analysis

To solve this problem, we have two attention Note.

A. The pointer exchange order.

             Assume List {1 2 3 4  5}

            pointer    p1 L0  1

                            p2  Ln  5

                            p3  Ln-1 4

             the reorder should be 1 5 2 3 4

             the opertation should   p3 = NULL;

                                                       p2.next = p1.next;

                                                        p1.next = p2;

             draw a picture about the order can help greatly.

B. Time

        We draw the conclusion we need to search the List n/2 times for the reorder the computation complexity should be O(n^2)

        This indeed waste much time.

        For the reorder List, L0→Ln→L1→Ln-1→L2→Ln-2→…

        We need the location of Ln, Ln-1, Ln-2.....This means if we know the their location  prior to reorder, we don't need to search the list again and again.

        Solution:

        Search the List and record their Location.

NOTE: This can be treated as the reducing the operation time at the expense of using more space.

3, CODE:

     Time Exceed Code:

      

if(head == NULL || head->next == NULL)
        return;
    ListNode *p1, *p2, *p3;
    std::vector<ListNode*> location;
    std::vector<ListNode*>::iterator it;
    p1 = head;
    while(p1 != NULL)
    {
        location.insert(location.begin(), p1);
        p1 = p1->next;
    }

    p1 = head;
    it = location.begin();
    p2 = *(it++);
    p3 = *(it);

    while(it!= location.end() && p2 != p1 && p2 != p1->next)
    {
        p3->next = NULL;
        p2->next = p1->next;
        p1->next = p2;

        p1 = p2->next;
        p2 = *(it++);
        p3 = *(it);
    }

AC code

if(head == NULL || head->next == NULL)
            return;
    ListNode *p1, *p2, *p3;
    std::vector<ListNode*> location;
    std::vector<ListNode*>::iterator it;
    p1 = head;
    while(p1 != NULL)
    {
        location.insert(location.begin(), p1);
        p1 = p1->next;
    }

    p1 = head;
    it = location.begin();
    p2 = *(it++);
    p3 = *(it);

    while(it!= location.end() && p2 != p1 && p2 != p1->next)
    {
        p3->next = NULL;
        p2->next = p1->next;
        p1->next = p2;

        p1 = p2->next;
        p2 = *(it++);
        p3 = *(it);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值