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 Solution
{
public:
    ListNode * reorderList(ListNode *head) {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode *slow = head;
        ListNode *fast = head;
        ListNode *cut = head;
        while (fast && fast->next)
        {
            cut = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        cut->next = NULL;

        slow = reverseList(slow);
        ListNode *result = mergeList(head, slow);
        return result;
    }

    ListNode* reverseList(ListNode *head) // 翻转链表
    {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode *prev = head;
        ListNode *curr = head->next;
        ListNode *temp = curr;
        prev->next = NULL;

        while (curr)
        {
            temp = curr->next;
            curr->next = prev;
            prev = curr;
            curr = temp;
        }
        return prev;
    }

    ListNode* mergeList(ListNode *head1, ListNode *head2)
    {
        ListNode* temp1 = head1;
        ListNode* temp2 = head2;
        ListNode* pMerge = head1;
        bool flag = true;

        while (temp1 != NULL && temp2 != NULL)
        {
            if (flag  && temp2 != NULL)
            {
                temp1 = head1->next;
                head1->next = temp2;
                head1 = head1->next;
                flag = false;
            }
            if (!flag  && temp1 != NULL)
            {
                temp2 = head1->next;
                head1->next = temp1;
                head1 = head1->next;
                flag = true;
            }
        }
        return pMerge;
    }
};

测试结果:

四.总结

这道题需要对链表进行翻转操作&对两个链表进行合并操作,实现本身不难,但代码只是随便一写,比较繁琐,需要后期再优化。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-14 08:13:45

leetcode笔记:Reorder List的相关文章

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 {

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