zoukankan      html  css  js  c++  java
  • Leetcode0143--Reorder List 链表重排

    【转载请注明】https://www.cnblogs.com/igoslly/p/9351564.html

    具体的图示可查看 链接


    代码一

    /**
     * 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) {
            if(head==NULL || head->next ==NULL) return;
            // two pointers split the list
            // 快慢指针,快指针到尾时,慢指针在前list尾部
            // example: 1->2->3->4->5 fast=5,slow=3, 1->2->3 & 4->5
            // example: 1->2->3->4    fast=3,slow=2, 1->2    & 3->4
            ListNode *slow = head, *fast = head;
            while(fast->next !=NULL && fast->next->next !=NULL){
                slow = slow ->next;
                fast = fast->next->next;
            }
            ListNode *head1 = head;
            // reverse the second list(with large numbers)
            // 翻转第二个链表
            ListNode *head2 = reverseList(slow->next);
            slow->next = NULL;
            while(head2!=NULL){             // list1 size >= list2 size
                ListNode *next1 = head1->next;
                ListNode *next2 = head2->next;
                head1->next = head2;
                head2->next = next1;
                head1 = next1;
                head2 = next2;
            }
            if(head1!=NULL){
                head1->next = NULL;
            }
        }
        // reverse list
        ListNode *reverseList(ListNode *head){
            if(head==NULL || head->next ==NULL) return head;
            ListNode * new_head = NULL;
            while(head!=NULL){
                ListNode *pNext = head->next;
                head->next = new_head;
                new_head = head;
                head = pNext;
            }
            return new_head;
        }
    };

    /**
     * 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) {
            stack<ListNode* > nodestack;
            int length=0;
            // 计算链表长度,结点压入stack
            ListNode *temp = head;
            while(temp){
                length++;
                nodestack.push(temp);
                temp = temp->next;
            }
            // 栈弹出,连接链表
            temp = head;
            int cnt = length/2;
            while(cnt){
                ListNode *head2 = nodestack.top();
                nodestack.pop();
                ListNode *headNext = temp->next;
                temp->next =head2;
                head2->next = headNext;
                temp = headNext;
                cnt--;
            }
            // 总数为odd,temp指向末尾元素
            // 总数为even,temp会和前元素重复,此处删除
            if(length%2){
                temp->next = NULL;
            }else{
                temp = NULL;
            }
        }
    };
  • 相关阅读:
    极高效内存池实现 (cpu-cache)
    gles2.0环境的在windows上的建立
    使用OpenGL绘制 shapefile文件 完成最基本的gis操作
    纯C++安卓开发 (ndk)系列之 ---- 常见问题
    如何用 纯C++(ndk)开发安卓应用 ?
    Android-NDK处理用户交互事件
    图解-安卓中调用OpenGL
    图解安卓-c++开发-通过java 调用c++ jni的使用
    搭建安卓开发环境 hello world andriod
    关于socket通讯,如何才能高效?
  • 原文地址:https://www.cnblogs.com/igoslly/p/9351564.html
Copyright © 2011-2022 走看看