有一个链表,奇数位升序偶数位降序,如何将链表变成升序

public class
OddIncreaseEvenDecrease {
    /**
     * 按照奇偶位拆分成两个链表
     * @param head
     * @return
     */
    public static Node[] getLists(Node head){
        Node head1 = null;
        Node head2 = null;
        
        Node cur1 = null;
        Node cur2 = null;
        int count = 1;//用来计数
        while(head != null){
            if(count % 2 == 1){
                if(cur1 != null){
                    cur1.next = head;
                    cur1 = cur1.next;
                }else{
                    cur1 = head;
                    head1 = cur1;
                }
            }else{
                if(cur2 != null){
                    cur2.next = head;
                    cur2 = cur2.next;
                }else{
                    cur2 = head;
                    head2 = cur2;
                }
            }
            head = head.next;
            count++;
        }
        //跳出循环,要让最后两个末尾元素的下一个都指向null
        cur1.next = null;
        cur2.next = null;
        
        Node[] nodes = new Node[]{head1,
head2};
        return nodes;
    }
    
    /**
     * 反转链表
     * @param head
     * @return
     */
    public static Node reverseList(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
     
    /**
     * 合并两个有序链表
     * @param head1
     * @param head2
     * @return
     */
    public static Node CombineList(Node head1,
Node head2){
        if(head1 == null || head2 == null){
            return head1 != null ? head1 :
head2;
        }
        Node head = head1.value < head2.value ?
head1 : head2;
        Node cur1 = head == head1 ? head1 :
head2;
        Node cur2 = head == head1 ? head2 :
head1;
        Node pre = null;
        Node next = null;
        while(cur1 != null && cur2 !=
null){
            if(cur1.value <= cur2.value){//这里一定要有=,否则一旦cur1的value和cur2的value相等的话,下面的pre.next会出现空指针异常
                pre = cur1;
                cur1 = cur1.next;
            }else{
                next = cur2.next;
                pre.next = cur2;
                cur2.next = cur1;
                pre = cur2;
                cur2 = next;
            }
        }
        pre.next = cur1 == null ? cur2 : cur1;
        
        return head;
    }
    
}

在C++中,我们可以使用结构体定义链表节点,并提供相应的函数来操作链表。以下是基本步骤: 1. **创建链表节点**: ```cpp struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; ``` 2. **创建无序链表**: ```cpp ListNode* createList(vector<int>& nums) { ListNode* head = nullptr; for (int num : nums) { head = insert(head, num); } return head; } // 插入节点函数 ListNode* insert(ListNode* head, int val) { ListNode* newNode = new ListNode(val); if (!head) { head = newNode; } else { ListNode* current = head; while (current->next) { current = current->next; } current->next = newNode; } return head; } ``` 3. **链表结点输出**: ```cpp void printList(ListNode* head) { ListNode* temp = head; while (temp != nullptr) { cout << temp->val << " "; temp = temp->next; } cout << endl; } ``` 4. **升序排序**: ```cpp ListNode* sortList(ListNode* head) { // 使用归并排序等算法对链表进行排序 // 这里仅提供思路,实际代码需要实现链表比较和合并操作 } ``` 5. **链表结点插入**: 已经在`createList`函数中包含插入操作。 6. **链表逆序**: ```cpp ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; ListNode* nextTemp = nullptr; while (curr) { nextTemp = curr->next; curr->next = prev; prev = curr; curr = nextTemp; } return prev; } ``` 7. **链表拆分**: ```cpp void splitList(ListNode* head) { ListNode* evenHead = nullptr, *evenTail = nullptr; ListNode* oddHead = nullptr, *oddTail = nullptr; ListNode* current = head; while (current) { if (current->val % 2 == 0) { if (!evenHead) { evenHead = current; evenTail = evenHead; } else { evenTail->next = current; evenTail = evenTail->next; } } else { if (!oddHead) { oddHead = current; oddTail = oddHead; } else { oddTail->next = current; oddTail = oddTail->next; } } current = current->next; } if (evenTail) { evenTail->next = nullptr; } if (oddTail) { oddTail->next = nullptr; } } ``` 8. **释放链表**: ```cpp void freeList(ListNode* head) { while (head) { ListNode* temp = head; head = head->next; delete temp; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值