剑指Offer——链表问题

剑指Offer——链表问题

1、第3题 从尾到头打印链表

题目描述:

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

基本思路:

1、链表原地翻转。
2、翻转之后进行遍历

代码实现:

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> res;
        if(!head)
            return res;
        ListNode * node = NULL;
        ListNode * cur = head;
        while(cur)
        {
            ListNode * temp = cur->next;
            cur->next = node;
            node = cur;
            cur = temp;
        }
        cur = node;
        while(cur)
        {
            res.push_back(cur->val);
            cur = cur->next;
        }
        return res;
    }
};
2、第14题 链表中倒数第k个节点

题目描述:

输入一个链表,输出该链表中倒数第k个结点。

基本思路:

  1. 计算链表长度
  2. 计算倒数第k个节点的位置

代码实现:

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
            if(pListHead == NULL || k<=0)
                return NULL;
            int length=0;
            ListNode * cur = pListHead;
            while(cur)
            {
                length ++;
                cur = cur->next;
            }
            if(k>length)
                return NULL;
            int index = length - k + 1;
            cur = pListHead;
            for(int i=1;i<index;i++)
                cur = cur->next;
            return cur;
    }
};
3、第15题 翻转链表

题目描述:

输入一个链表,反转链表后,输出新链表的表头。

基本思路:
在原有的链表的基础上,不断翻转链表的每一个节点,返回结果即可。

代码实现:

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
            if(pHead == NULL)
                return NULL;
            ListNode * node = NULL;
            ListNode * cur = pHead;
            while(cur)
            {
                ListNode * temp = cur->next;
                cur->next = node;
                node = cur;
                cur = temp;
             }
        return node;
    }
};
4、第16题、合并两个排序链表

题目描述:

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

基本思路:

该题与归并偏排序中的合并过程类似,借助辅助头部,两个指针,将值小的放到辅助头部的后面。

代码实现:

class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if(!pHead1)
            return pHead2;
        if(!pHead2)
            return pHead1;
        ListNode * dummy = new ListNode(-1);
        ListNode * cur = dummy;
        ListNode * cur1 = pHead1;
        ListNode * cur2 = pHead2;
        while(cur1&&cur2)
        {
            if(cur1->val <= cur2->val)
            {
                cur->next = cur1;
                cur = cur->next;
                cur1 = cur1->next;
            }
            else
            {
                cur->next = cur2;
                cur = cur->next;
                cur2 = cur2->next;
            }
        }
        if(cur1)
            cur->next = cur1;
        if(cur2)
            cur->next = cur2;
        return dummy->next;
    }
};
5、第25题 复杂链表的复制

题目描述:

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

基本思路:

  1. 链表每一个节点复制。
  2. 链表每一个节点的随机指针的复制。
  3. 将新的链表从原始链表中拆解下来。

代码实现:

class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead)
    {
        if(pHead == NULL)
            return NULL;
        RandomListNode * cur = pHead;
        while(cur)
        {
            RandomListNode * node = new RandomListNode(cur->label);
            RandomListNode * temp = cur->next;
            cur->next = node;
            node->next = temp;
            cur = temp;
        }
        cur = pHead;
        while(cur)
        {
            RandomListNode * node = cur->next;
            if(cur->random)
                node->random = cur->random->next;
            cur = node->next;
        }
        RandomListNode * pCloneHead = pHead->next;
        cur = pHead;
        while(cur->next)
        {
            RandomListNode * tmp = cur->next;
            cur->next = tmp->next;
            cur = tmp;
        }
        return pCloneHead;
    }
};
6、第36题、两个链表的第一个公共节点

题目描述:

输入两个链表,找出它们的第一个公共结点。(注意因为传入数据是链表,所以错误测试数据的提示是用其他方式显示的,保证传入数据是正确的)

基本思路:

首先,我们能够确定两个链表不是有环的,所有两个链表而言,其长度的差是前面没有重合的长度之差,所以,只需要确定差值之后,将两个链表的指针放到初始和差值的位置,同时向前遍历,找到公共节点即可。

代码实现:

class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        if(pHead1 == NULL || pHead2 == NULL)
            return NULL;
        int length1 = 0;
        int length2 = 0;
        int delta = 0;
        ListNode * cur1 = pHead1;
        ListNode * cur2 = pHead2;
        while(cur1)
        {
            length1 ++ ;
            cur1 = cur1->next;
        }
        while(cur2)
        {
            length2 ++;
            cur2 = cur2->next;
        }
        cur1 = pHead1;
        cur2 = pHead2;
        if(length1 > length2)
        {
            delta = length1 - length2;
            for(int i=0;i<delta;i++)
                cur1 = cur1->next;
            while(cur1 != cur2)
            {
                cur1 = cur1->next;
                cur2 = cur2->next;
            }
        }
        else
        {
            delta = length2 - length1;
            for(int i=0;i<delta;i++)
                cur2 = cur2->next;
            while(cur1 != cur2)
            {
                cur1 = cur1->next;
                cur2 = cur2->next;
            }
        }
        return cur1;
    }
};
7 第55题 链表中环的入口节点

题目描述:

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

基本思路:
本题的核心是采用快慢指针的方式,如果链表是有环的,现在将链表分成三个部分,第一个部分是由起始节点到环入口节点的长度为a,第二个部分是从入口节点到快慢指针在环中的交点的长度为b,从交点到环入口节点的距离为c,则有慢指针所走的长度为:a+b,快指针所走的距离为a+b+c+b,同时快指针所走的距离是慢指针的两倍,则有2(a+b) = a+b+b+c,则有 a=c,则首先用快慢指针找到交点P,然后第一个指针从起始位置开始,第二个指针从交点开始,走到相交的位置即可。

代码实现:

class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead)
    {
        if(pHead==NULL)
            return NULL;
        ListNode * slow = pHead;
        ListNode * fast = pHead;
        while(fast && fast->next)
        {
            fast = fast->next->next;
            slow = slow->next;
            if(slow==fast)
            {
                ListNode * cur = pHead;
                while(cur!=slow)
                {
                    cur = cur->next;
                    slow = slow->next;
                }
                return cur;
            }
        }
        return NULL;
    }
};
8 第56题 删除链表中的重复节点

题目描述:

在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

基本思路:

本题的主要思路分成两个部分,第一个部分是增加一个额外的头,防止第一个元素就是重复的元素,删除之后找不到头结点。

第二个部分是双指针的操作,第一个指针是当前指针cur,第二个指针失cur的前一个指针pre。移动cur指针,越过重复元素,将pre指针的下一个指针指向cur。

代码实现:

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
            if(!pHead)
                return NULL;
            ListNode * dummy = new ListNode(-1);
            dummy->next = pHead;
            ListNode * pre = dummy;
            ListNode * cur = pHead;
            while(cur)
            {
                if(cur->next && cur->val == cur->next->val)
                {   
                    while(cur->next && cur->val == cur->next->val)
                        cur = cur->next;
                    pre->next = cur->next;
                    cur = cur->next;
                }
                else
                {
                    pre = pre->next;
                    cur = cur->next;
                }
            }
        return dummy->next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值