链表相关练习题(C++)

本文主要探讨了C++实现链表的相关练习题目,包括链表的基本操作、复杂问题的解决策略,适合想要提升链表操作技能的C++程序员阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、链表
1、在节点值有序的单循环链表中插入指定结点,使插入后的链表依然有序。
解法:一下解法只是单链表,不是循环链表。

class  InsertValue {
public
:

    ListNode* buildLinkedList(vector< int > &A,vector< int > &nextPoint) {
         int  len = A.size();
        ListNode* h = new ListNode(A[ 0 ]);        
         if  (len ==  1 ) {
            h->next = h;
             return  h;
        }             
         else  {
            ListNode* p = h;

             for  ( int  i =  1 ,k =  0 ;i < len;i++) {
                p->next =  new  ListNode(A[nextPoint[k]]);
                p = p->next;
                k = nextPoint[k];
        }
      }
         return  h;
    }

    ListNode* insert(vector< int > A, vector< int > nxt,  int  val) {
        ListNode* insertNode =  new  ListNode(val);
         if  (A.empty()) {
            insertNode->next = insertNode;
             return  insertNode;
        }

        ListNode* head = buildLinkedList(A,nxt);        
        ListNode* p = head;
        ListNode* q = head->next;

         for ( int  i =  1 ; i < A.size(); i++) {
             if  ((val >= p->val && val <= q->val) || (val <= p->val && val >= q->val)) 
                 break ;
            p = q;
            q = q->next;
        }                
        insertNode->next = q;
        p->next = insertNode;                   
         return  head;
    }
};

2、对于一个链表,我们需要用一个特定阈值完成对它的分化,使得小于等于这个值的结点移到前面,大于该值的结点在后面,同时保证两类结点内部的位置关系不变。
解法:遍历链表,将链表分成两个子链表,其中一个链表的元素值都比阈值小,另一个比阈值都大,再将两个链表连接起来即可。
class  Divide {
public :
    ListNode* listDivide(ListNode* head,  int  val) {
         if  (!head) 
             return  NULL;
         if  (!head->next)
             return  head;

        ListNode *head1,*head2,*h1,*h2,*p,*q;
        head1 = head2 = h1 = h2 = NULL;
        p = head;
        q = head->next;

        while (p) {
             if  (p->val <= val) {
                 if  (!head1) {
                    head1 = p;
                    h1 = head1;
                }
                 else  {
                    h1->next = p;
                    h1 = h1->next;
                }                                
            }
             else  {
                 if  (!head2) {
                    head2 = p;
                    h2 = head2;
                }
                 else  {
                    h2->next = p;
                    h2 = h2->next;
                }                                                                        
            }
            p->next = NULL;  
            p = q;
            if (q != NULL)
                q = q->next;
        }

         if  (head1->val <= val) {
            h1->next = head2;
             if  (h2 != NULL)
                h2->next = NULL;
             return  head1;
        }            
         else  {
            h2->next = head1;
             if  (h1 != NULL)
                h1->next = NULL;
             return  head2;
        }
    }
};

3、打印两个有序链表的公共部分
解法:同时遍历两个链表,逐个比较。相等则打印,两个指针同时后移;不相等则哪一个较小就将其对应指针后移,另一个指针不动;其中一个指针为空时停止比较。
class Common {
public :
     vector< int > findCommonParts(ListNode* headA, ListNode* headB) {
         vector< int > common;
         if (!headA || !headB)
             return common;       
         ListNode *p,*q;
         p = headA;
         q = headB;
         while (p && q) {
             if (q->val == p->val) {
                 common.push_back(q->val);
                 q = q->next;
                 p = p->next;
             }
             else if (q->val > p->val)
                 p = p->next;
             else
                 q = q->next;               
         }
        
头歌C++中的链表是一种基础数据结构,常用于需要频繁插入和删除元素的场景。下面是一个关于链表的强化练习题目示例: **题目:单链表反转** 给定一个单向链表,按节点值从大到小排序,并返回一个新的链表,新链表的每个节点值等于原链表中对应位置上所有节点值的最大值。 **答案示例:** ```cpp #include <iostream> #include <vector> struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }; ListNode* reverseList(ListNode* head) { if (!head || !head->next) return head; // 链表为空或只有一个节点,直接返回 ListNode* prev = nullptr, *current = head, *temp = nullptr; while (current) { temp = current->next; current->next = prev; // 翻转节点指针 prev = current; current = temp; } return prev; // 返回新的头节点 } void printList(ListNode* head) { while (head) { std::cout << head->val << " -> "; head = head->next; } std::cout << "nullptr" << std::endl; } int main() { // 创建链表示例 ListNode* list = new ListNode(5); list->next = new ListNode(2); list->next->next = new ListNode(8); list->next->next->next = new ListNode(3); printList(list); // 输出:3 -> 8 -> 5 -> 2 -> nullptr list = reverseList(list); printList(list); // 输出:8 -> 5 -> 3 -> 2 -> nullptr return 0; } ``` 在这个例子中,我们首先创建了一个链表并打印出原始顺序。然后通过`reverseList`函数将其反转,最后再次打印结果,可以看到链表已经按照节点值从大到小排列了。 **相关问题--:** 1. 除了反转,链表还有哪些常见的操作? 2. 如何在链表中查找特定值的第一个节点? 3. 怎么处理链表的循环问题?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值