《剑指offer》中链表相关题目的c++实现

LinkedList

面试题06 - 从尾到头打印链表(栈;递归)
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
//----设置栈
class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        stack<int>stk; //----设置栈
        while(head){
  
          stk.push(head->val);
            head = head->next;
        }
        vector<int>res; //----放置答案
        while(!stk.empty()){
            res.push_back(stk.top());  //----栈顶元素放入容器
            stk.pop(); //----栈顶元素弹出
        }
        return res;
    }
};
//----递归
class Solution {
public:
    vector<int> res;
    vector<int> reversePrint(ListNode* head) {
        if (head != NULL){
            reversePrint(head->next);
            res.push_back(head->val);
        }
    return res;
    }
};
面试题22 - 链表中倒数第k个结点(快慢指针)
//----快慢指针
class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {
        if(head==NULL || k==0){
            return NULL;
        }

        ListNode *pFast = head;
        ListNode *pSlow = NULL;

        for(unsigned int i = 0; i<k-1; ++i){
            if(pFast->next != NULL)
                pFast = pFast->next;
            else
                return NULL;
        }

        pSlow = head;
        while (pFast->next != NULL){
            pFast = pFast->next;
            pSlow = pSlow->next;
        }
        
        return pSlow;
    }
};
面试题24-反转链表(三个指针)
//----三个指针
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = NULL;
        ListNode *cur = head;

        while (cur) {
            ListNode *tmp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
};
面试题25-合并两个排序的链表(归并排序;递归)
//----归并排序+dummy指针
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
				if(l1 == NULL)
						return l2;
				else if(l2 == NULL)
						return l1;

        ListNode *dummy = new ListNode(0); //新建一个虚拟的头结点,用来向后接新的链表
        ListNode *cur = dummy;

        while(l1!=NULL && l2!=NULL){
            if(l1->val <= l2->val){
                cur->next = l1;
                l1 = l1->next;
            }
            else{
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        cur->next = (l1 != NULL)? l1 : l2; 

        return dummy->next;
    }
};
//----递归
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (!l1) return l2;
        if (!l2) return l1;
        ListNode* result = NULL;
        if(l1->val < l2->val) {
            result = l1;
            result->next = mergeTwoLists(l1->next, l2);
        }
        else {
            result = l2;
            result->next = mergeTwoLists(l1, l2->next);
        }
        return result;
    }
};
面试题35-复杂链表的复制(double结点,再拆分)
//----double结点,再拆分
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(head == NULL){
            return NULL;
        }

        //复制结点的val,并放到原结点的后面
        Node* cur = head;
        while(cur){
            Node* curCopy = new Node(cur->val);
            curCopy->next = cur->next;
            
            cur->next = curCopy;
            cur = curCopy->next;
        }

        //构造random
        cur = head;
        while(cur){
            if(cur->random){
                cur->next->random = cur->random->next;
            }
            cur = cur->next->next;
        }
        //拆分链表
        cur = head;
        Node* newHead = head->next; //复制链表的头节点
        while(cur->next){
            Node* tmp = cur->next;
            cur->next = tmp->next;
            cur = tmp;
        }
        
        return newHead;
    }
};
面试题18-删除链表的节点(简单遍历,修改指针)
//----简单遍历,修改指针
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteNode(ListNode* head, int val) {
       ListNode* dummy = new ListNode(-1);
       dummy->next = head; 
       ListNode* pre = dummy;
       while(pre->next != NULL){
           if(pre->next->val == val){
               pre->next = pre->next->next;
               break;
           }
           pre = pre->next;
       }
       return dummy->next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值