1.剑指刷题 链表

剑指3 从尾到头打印链表

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> print;
        stack<ListNode *> s;
        ListNode * pNode = head;
        while(pNode != nullptr)
        {
            s.push(pNode);
            pNode = pNode->next;
        }
        while(!s.empty())
        {
            print.push_back(s.top().);
             s.pop();
        }
          return print;
    }
};

剑指14 链表中倒数第k个节点

JZ14 链表中倒数第k个节点
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        //双指针
        if(pListHead == nullptr||k==0)
        {
            return nullptr;
        }
        ListNode * Ahead = pListHead;
        ListNode * Behind = nullptr;
        //第一个指针先走k-1步
        for (int i = 0;i<k-1;i++)
        {
            if(Ahead->next !=nullptr)
                Ahead = Ahead->next;
            else
                return nullptr;
        }
        Behind = pListHead;
        while(Ahead->next != nullptr)
        {
            Ahead = Ahead->next;
            Behind = Behind->next;
        }
    return Behind;
    }
};

剑指15 反转链表

JZ15 反转链表
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode *pNode = pHead;
        ListNode *pPre = nullptr;
        ListNode *pReverHead = nullptr;
        while (pNode != nullptr)
        {
            ListNode *pNext = pNode->next;
            if(pNext ==nullptr)
            {
                pReverHead = pNode;
            }
            pNode->next = pPre;
            pPre = pNode;
            pNode = pNext;
        }
        return pReverHead;
    }
};

剑指25 复杂链表的复制

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        //复制节点
        RandomListNode *pNode = pHead;
        
        while(pNode !=nullptr)
        {
            RandomListNode * ClonedNode = new RandomListNode(pNode->label);
            ClonedNode->next = pNode->next;
            pNode->next = ClonedNode;
            pNode = ClonedNode->next;
            
        }
        //设置随机指针
        pNode = pHead;
        RandomListNode *ClonedNode = pNode->next;
        while(pNode !=nullptr)
        {
            if(pNode->random !=nullptr)
            {
                ClonedNode->random = pNode->random->next;
            }
            pNode = ClonedNode->next;
            ClonedNode = pNode->next;
        }
        //奇偶分拆
        pNode = pHead;
        RandomListNode *ClonedHead = nullptr;
        ClonedNode = nullptr;
        
        if(pNode != nullptr)
        {
            ClonedHead = ClonedNode = pNode->next;
            pNode->next = ClonedNode->next;
            pNode = pNode->next;
        }
        while(pNode != nullptr)
        {
            ClonedNode->next = pNode->next;
            ClonedNode = pNode->next;
            pNode->next = ClonedNode->next;
            pNode = ClonedNode->next;
        }
        return ClonedHead;
    }
};

剑指36 两个链表的第一个公共结点

JZ36 	两个链表的第一个公共结点
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) 
    {
        ListNode *l1 = pHead1;
        ListNode *l2 = pHead2;
        int length1 = 0;
        int length2 = 0;
        int lengdif = 0;
        
        while (l1 != nullptr)
        {
            length1++;
            l1 = l1->next;
        }
         while (l2 != nullptr)
        {
            length2++;
            l2 = l2->next;
        }
        //判断哪个链表长
            ListNode *Longlist ;
            ListNode *Shortlist;
        if(length1>length2)
        {
           Longlist = pHead1;
           Shortlist = pHead2;
           lengdif = length1 - length2;
        }
        else
        {
            Longlist = pHead2;
            Shortlist  = pHead1;
           lengdif = length2 - length1;
        }
        //长链表先走lengdif步
       for(int i = 0;i<lengdif;i++)
       {
           Longlist = Longlist->next;
       }
        //找节点
        while(Longlist !=nullptr && Shortlist !=nullptr)
           {
            if(Longlist == Shortlist)
            {
                return Longlist;
            }
            Longlist = Longlist->next;
            Shortlist = Shortlist->next;
            }
          }
};

剑指55 链表中环的入口结点

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead)
    {
        //先判断是否有环
        ListNode *pNode = pHead;
        ListNode *MeetNode = nullptr;
        if(pNode == nullptr)
            return nullptr;
        ListNode *pSlow = pNode->next;
        if(pSlow == nullptr)
            return nullptr;
        

        ListNode *pFast = pSlow->next;
        
        while(pSlow !=nullptr &&pFast != nullptr)
        {
            if (pSlow == pFast)
            {
              MeetNode = pSlow;
                break;
            }
            pSlow = pSlow->next;
            pFast = pFast->next;
            if(pFast != nullptr)
             pFast = pFast->next;
        }
        //确定环的大小 存在length
        if(MeetNode == nullptr)
            return nullptr;
        ListNode * pNode1 = MeetNode;
        int length = 1;
        while (pNode1->next != MeetNode)
        {
            pNode1 = pNode1->next;
            ++length;
        }
        pNode1 = pHead;
        ListNode *pNode2 = pHead;
        for(int i=0;i<length;i++)
        {
            pNode1 = pNode1->next;
        }
        while (pNode1 != nullptr && pNode2!=nullptr)
        {
            if(pNode1 == pNode2)
              return pNode1;
            pNode1 = pNode1->next;
            pNode2 = pNode2->next;
        }

    }
};

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值