leetcode笔记—Reorder List

给一个链表 L: L0→L1→…→Ln-1→Ln,

需要返回: L0→Ln→L1→Ln-1→L2→Ln-2→…

思路1:

void reorderList(ListNode* head) {
       ListNode* p=head;
        while(p&&p->next)
        {
            ListNode* pnext=p->next;   //记下下一个节点
            ListNode* temp=p;
            if(p->next->next==NULL)
            {
                p=p->next;
            }
            else
            {
                while(temp->next&&temp->next->next)
                {
                    temp=temp->next;

                }//让temp指向倒数第二个节点
                p->next=temp->next;  //指向最后一个节点
                temp->next=NULL;    //删除最后一个节点
                p=p->next;          //连接后面的节点
                p->next=pnext;
                p=p->next;

            }
        }
        if(p)
        {
            p->next=NULL;
     }}

思路2:

if(head == NULL){
            return;
        }  

        ListNode* p1 = head;
        ListNode* p2 = splitList(head);  

        p2 = revertList(p2);  

        mergeList(p1, p2);
    }  

    void mergeList(ListNode * p1, ListNode * p2){  //合并链表
        while(p2 != NULL){
            ListNode* tmp = p2;
            p2 = p2->next;  

            tmp->next = p1->next;
            p1->next = tmp;
            p1 = p1->next;
            p1 = p1->next;
        };
    }  

    ListNode* splitList(ListNode *head){  //去后半部分链表
        ListNode* slow = new ListNode(0);
        slow->next = head;
        ListNode* fast = slow;  

        while(fast->next != NULL && fast->next->next != NULL){
            slow = slow->next;
            fast = fast->next;
            fast = fast->next;
        }  

        if(fast->next != NULL){
            slow = slow->next;
            fast = fast->next;
        }  

        ListNode* tmp = slow->next;
        slow->next = NULL;
        return tmp;
    }  

    ListNode* revertList(ListNode* head){  //翻转链表
        if(head == NULL){
            return NULL;
        }  

        ListNode* p = head->next;
        head->next = NULL;  

        while(p != NULL){
            ListNode* tmp = p;
            p = p->next;
            tmp->next = head;
            head = tmp;
        }
        return head;
    }
				
时间: 2024-10-24 13:16:16

leetcode笔记—Reorder List的相关文章

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&

leetcode笔记:Pascal's Triangle

一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 二. 题目分析 关于帕斯卡三角形的定义,可參考:http://baike.baidu.com/link?url=qk_-urYQnO4v6v3P4BuMtCa0tMNUqJUk

[leetcode笔记] Longest Consecutive Sequence

随机挑选一题试试手气.   问题描述: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your al

leetcode笔记:Reorder List

一.题目描述 二.题目分析 直接按照题目的要求求解,设ListNode *head为待处理的链表,算法包括以下步骤: 1. 将链表head分为前后两部分,前半部分链表head1 和后半部分链表head2: 2. 将后半段链表head12做逆序操作: 3. 合并head1, head2: 三.示例代码 struct ListNode { int value; ListNode *next; ListNode(int x) : value(x), next(NULL){}; }; class Sol

【Leetcode】Reorder List JAVA

一.题目描述 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.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

[LeetCode] String Reorder Distance Apart

Question: Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other. Input: { a, b, b }, distance = 2 Output: { b, a, b } http://leetcode.com/2010/05/here-is-another-google-phone-interv

[LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

You have an array of?`logs`.? Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric?identifier.? Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word a

LeetCode 笔记26 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 没辙,智商碾压题.楼主没遇到之前就只会这种做法. public int si