66-Reorder List

  1. Reorder List My Submissions QuestionEditorial Solution

    Total Accepted: 64392 Total Submissions: 281830 Difficulty: Medium

    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}.

思路:

1.找到中点,前半部分最后一个点

a.找到后半部分第一个点

b.后半部分入栈

c.遍历前半部分,间隔性依次链接栈中节点

时间复杂度:

空间复杂度:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        int len=0;
        if(head==NULL||head->next==NULL)return;
        if(head->next->next==NULL)return;
        ListNode *p=head,*fir_end,*sec_beg;
        while(p){
            len++;
            p = p->next;
        }
        int mid=(len%2==0)?len/2:len/2+1;
        mid = mid -1;
        fir_end = head;
        while(mid--){
            fir_end=fir_end->next;
        }
        sec_beg = fir_end->next;
        stack<ListNode*> slist;
        while(sec_beg!=NULL){
            slist.push(sec_beg);
            sec_beg = sec_beg->next;
        }
        while(!slist.empty()){  //如果栈中有元素未被链接起来
            ListNode *tmp=head->next,*stop=slist.top(); //保存前半部分当前节点下一个节点
            head->next = stop;   //链接栈中元素
            stop->next = tmp;    //栈中元素链接到原来当前节点的下一元素,相当于在中间插入
            slist.pop();
            head = tmp;
        }
        if(head!=NULL)head->next = NULL;//如果链长为奇数,最后一个元素指向空
    }
};
时间: 2024-11-13 09:10:33

66-Reorder List的相关文章

leetcode143 Reorder List

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}. 1 /** 2 * Definition for singly-linked list. 3 *

Reorder List (12)

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}. 思路: 利用快慢指针的方式将链表分为前后两个部分,然后将后半部分的链表倒序重排,然后再每次先从

62. 链表重排[Reorder List]

[本文链接] http://www.cnblogs.com/hellogiser/p/reorder-list.html [题目] 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,5,6,7},

leetcode-005 reorder list

1 package leetcode; 2 3 public class ReOrderList { 4 public void reorderList(ListNode head) { 5 if(head==null||head.next==null||head.next.next==null){ 6 7 }else{ 8 int l=numNode(head); 9 ListNode mid = new ListNode(-1); 10 mid=getMid(head); 11 ListNo

[LeetCode]Reorder List

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}. 这道题是将一个给定的单链表按照某种规则进行重排序,要求是不可以简单地直接交换结点中的值. 思路

安装部署VMware vSphere 5.5文档 (6-6) 集群和vMotion

部署VMware vSphere 5.5 实施文档 ############################################################################### ## ver1.0 2014-09-09 ## ## 本文内容来自中国专利信息中心-基础系统处-张阳 ## ## 如有转载,请务必保留本文链接及版权信息. ## ## QQ:962903 ## ## E-mail:[email protected]## #################

143. Reorder List

本周对链表操作进行了巩固 题目: 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}. 题解: 这道题虽然看上去很复杂,但经过分析,可知实质上这道题就是

【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. 在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他又和他人讨论起了二叉搜索树.什么是二叉搜索树呢?二叉搜索树首先是一棵二叉树.设key[p]表示结点p上的数值.对于其中的每个结点p,若其存在左孩子lch,则key

Reorder List

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}. 这是一道比较综合的链表题目.一开始拿到手足无措.慢慢分析了一下,其实做法无非分三步: 1.将链表分

[Linked List]Reorder List

otal Accepted: 54991 Total Submissions: 252600 Difficulty: Medium 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