zoukankan      html  css  js  c++  java
  • 如何实现单链表交换任意两个元素(不包括头结点)

    对于单链表而言,假设交换A、B两个节点,那么需要交换A与B的next指针以及A、B直接前驱的next指针。

    需要注意特殊情况:1、当A与B相邻时:A->next = B;或者B->next = A;

             2、当A和B元素相同时,则没有必要交换。

             3、A与B有一个节点是头结点,不需要交换。

    #include <iostream>
    #include <algorithm>
    #include "string.h"
    #include "stdio.h"
    #include <vector>
    #include <deque>
    #include<stack>
    using namespace std;
    
    struct ListNode {
        int val;
        struct ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    };
    class List{
    public:
    
        ListNode* CreatList(int* arr,int len)
        {
            int val;
            ListNode* pHead = new ListNode(arr[0]);
            ListNode* pCurrent=NULL;
            ListNode* rear = pHead;
            int count = 1;
            while(count<len)
            {
                ListNode* pCurrent = new ListNode(arr[count]);
                rear->next = pCurrent;
                rear = pCurrent;
                count++;
            }
            rear->next = NULL;
            return pHead;
        }
        void ShowList(ListNode* pHead)
        {
           while(pHead)
           {
               cout<<pHead->val<<" ";
               pHead = pHead->next;
           }
           cout<<endl;
        }
        ListNode* GetLastNode(ListNode* pHead)
        {
            ListNode* pNode = pHead;
            while(pNode->next!=NULL)
            {
                pNode=pNode->next;
            }
            return pNode;
        }
    };
    class Sort{
    public:
        ListNode* changeList(ListNode* pHead,ListNode* pNode1,ListNode* pNode2)
        {
            if(pHead == NULL|| pNode1 == NULL || pNode2 == NULL)
                return pHead;
            //结点pNode1 等于pNode2
            if(pNode1->val == pNode2->val)
                return pHead;
            if(pNode1->next == pNode2)
            {
                ListNode* pre = FindPre(pHead,pNode1);
                if(pre == NULL)
                    return pHead;
                pre->next = pNode2;
                pNode1->next = pNode2->next;
                pNode2->next = pNode1;
            }
            else if(pNode2->next == pNode1)
            {
                ListNode* pre = FindPre(pHead,pNode2);
                if(pre == NULL)
                    return pHead;
                pre->next = pNode1;
                pNode2->next = pNode1->next;
                pNode1->next = pNode2;
            }
            else if(pNode1!=pNode2){
                ListNode* pre1 = FindPre(pHead,pNode1);
                ListNode* pre2 = FindPre(pHead,pNode2);
                ListNode* next1 = pNode1->next;
                ListNode* next2 = pNode2->next;
                pre1->next = pNode2;
                pNode2->next = next1;
                pre2->next = pNode1;
                pNode1->next = next2;
            }
            return pHead;
        }
    
        ListNode* FindPre(ListNode* pHead,ListNode* pNode)
        {
            ListNode* p = pHead;
            while(p)
            {
                if(p->next == pNode)
                    return p;
                p = p->next;
            }
            return NULL;
        }
    
    };
    int array[7];
    int n;
    int number;
    int first;
    int second;
    int main()
    {
        cin>>n;
    
        for(int i=0;i<n;i++)
        {
            cin>>number;
            array[i]=number;
        }
    
        cin>>first;
        cin>>second;
    
        List list;
        Sort sort;
        ListNode* pHead1 = list.CreatList(array,sizeof(array)/sizeof(array[0]));
        ListNode* pNode1 = pHead1;
        ListNode* pNode2 = pHead1;
        for(int i=1;i<first;i++)
        {
            pNode1=pNode1->next;
        }
        cout<<pNode1->val<<endl;
        for(int i=1;i<second;i++)
        {
            pNode2 = pNode2->next;
        }
        cout<<pNode2->val<<endl;
        list.ShowList(pHead1);
        ListNode* p = sort.changeList(pHead1,pNode1,pNode2);
        //cout<<pEnd->val<<endl;
        list.ShowList(p);
        return 0;
    }
  • 相关阅读:
    mysql查询太慢,我们如何进行性能优化?
    详解MySQL事务原理
    精通MySQL之锁篇
    精通MySQL之索引篇,这篇注重练习!
    mysql数据结构及mvcc
    mysql-主从同步
    redis分布式锁
    售后系统构建和商品状态重构经验与总结
    【更新2021-4-25】如何处理PoshSSH 连接主机时的"New-SSHSession : Key exchange negotiation failed."故障
    计算几何---大牛们
  • 原文地址:https://www.cnblogs.com/omelet/p/6635548.html
Copyright © 2011-2022 走看看