链表算法题

1.链表反转

leetcode链接
利用栈先入先出

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) 
    {
        stack<int> st;//利用栈
        ListNode* p = head;
        while(p)
        {
            st.push(p->val);
            p = p->next;
        }
        p = head;
        while(p)
        {
           p->val = st.top();
           st.pop();
           p = p->next;
        }
        p = head;//需要找到头节点的位置
      return p;
    }
};

双指针,画图解决

 ListNode* reverseList(ListNode* head) 
    {
       ListNode *cur = NULL ,*pre = head;
       while(pre)
       {
           ListNode* tmp = pre->next;
           pre->next = cur;
           cur = pre;
           pre = tmp;
       }
       return cur;
    }
 

在这里插入图片描述


2.合并两个排序的链表

leetcode链接

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 
    {
      ListNode* head = new ListNode(1);
    ListNode* r = head;
    while(l1 != NULL && l2 != NULL)
    {
        if(l1->val <= l2->val)
        {
            head->next = l1;
            l1 = l1->next;
        } 
        else
        {
           head->next = l2;
           l2 = l2->next;
        }
         head = head->next;
    }
    head->next = l1 == NULL ? l2 : l1;
    return r->next;
    }
};

实现思想:定义一个新节点,比较俩个链表的值,取较小值迭代即可


3.链表带环

leetcode 环形链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) 
    {
        if(head == NULL || head->next == NULL)
        return false;
        ListNode* fast = head->next ,*show = head;
        while(fast != show)
        {
            if(fast->next==NULL||fast->next->next==NULL)
            return false;
            fast = fast->next->next;
            show = show->next;
        }
        return true;
    }
};

实现思想:快慢指针,直到找到相交点;


4.两个链表第一个公共节点

leetcode两个链表第一个公共节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
       if(headA == NULL || headB == NULL)
       return NULL;
       ListNode* n1 = headA;
       ListNode* n2 = headB;
       while(n1 != n2)
       {
           n1 = (n1 == NULL) ? headB : n1->next;
           n2 = (n2 == NULL)? headA : n2->next;
       }
       return n1;
    }
};

实现思想:走完自己的路后,再对方的路上再走一遍,公共节点个数相等,则走到非公共节点之和出恰好为公共节点;


5.复杂链表的复制

leetcode链接


class Solution {
public:
    
    //第一步,复制复杂指针的label和next
    void CloneNodes(RandomListNode* pHead){
        RandomListNode* pNode = pHead;
        while(pNode){
            //复制一个相同的结点
            RandomListNode* pCloned = new RandomListNode(0);
            pCloned->label = pNode->label;
            pCloned->next = pNode->next;
            pCloned->random = NULL;
            
            
            //将复制的结点插入原结点后面
            pNode->next = pCloned;
            pNode = pCloned->next;
        }
    }
    
    //第二步,处理复杂指针的random
    void ConnectSiblingNodes(RandomListNode* pHead){
        RandomListNode* pNode = pHead;
        while(pNode){
            RandomListNode* pCloned = pNode->next;
            
            //为复制的结点赋值其random,其random是原结点的random的一个复制的结点
            if(pNode->random != NULL){
                pCloned->random = pNode->random->next;
            }
            pNode = pCloned->next;
        }
    }
    
    
    //第三步,拆分链表
    RandomListNode* ReconnectNodes(RandomListNode* pHead){
        RandomListNode* pNode = pHead;        //原链表
        RandomListNode* pClonedHead = NULL;        //复制链表表头
        RandomListNode* pClonedNode = NULL;        //复制链表的结点
        
        
        //拆分表头
        if(pNode){
            pClonedHead = pNode->next;
            pClonedNode = pClonedHead;
            pNode->next = pClonedNode->next;
            pNode = pNode->next;
        }
        
        
        //拆分各结点
        while(pNode){
            pClonedNode->next = pNode->next;
            pClonedNode = pClonedNode->next;
            pNode->next = pClonedNode->next;
            pNode = pNode->next;
        }
        return pClonedHead;
    }
    
    RandomListNode* Clone(RandomListNode* pHead)
    {
        CloneNodes(pHead);
        ConnectSiblingNodes(pHead);
        return ReconnectNodes(pHead);
    }
};

解析:大佬解析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值