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;
    }
  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/omelet/p/6635548.html
Copyright © 2011-2022 走看看