【CODE】链表——牛客·剑指offer

从尾到头打印链表

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

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

链表中环的入口节点

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

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* EntryNodeOfLoop(ListNode* pHead){
        ListNode *fast=pHead;
        ListNode *slow=pHead;
        while(fast&&fast->next){
            fast=fast->next->next;
            slow=slow->next;
            if(fast==slow) break;
        }
        if(fast==NULL || fast->next==NULL) return NULL;
        slow=pHead;
        while(fast!=slow){
            fast=fast->next;
            slow=slow->next;
        }
        return fast;
    }
};

删除链表中重复的节点

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

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
        val(x), next(NULL) {
    }
};
*/
class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead){
        if(pHead==NULL) return pHead;
        ListNode *one=pHead,*two=pHead,*three=pHead;
        if(pHead->next!=NULL) two=pHead->next;
        else return pHead;
        if(pHead->next->next!=NULL) three=pHead->next->next;
        else if(one->val==two->val) return NULL;
        else return pHead;
        int flag=0;
        while(one->val==two->val){
            if(three->val!=two->val){
                *one=*three;//!!!???要加*
                if(one->next){
                    two=one->next;
                    if(two->next) three=two->next;
                    else{
                        if(one->val==two->val) return NULL;
                        else return one;
                    }
                }else return one;
            }else{
                if(three->next) three=three->next;
                else return NULL;
            }
        }
        while(three!=NULL && two!=NULL && one!=NULL){
            if(two->val==three->val){
                two->next=three->next;
                three=three->next;
                flag=1;
            }else if(flag==1){
                one->next=two->next;
                two=two->next;
                three=three->next;
                flag=0;
            }else{
                one=one->next;
                two=two->next;
                three=three->next;
            }
        }
        if(flag==1){
            one->next=two->next;
        }
        return pHead;
    }
};
发布了133 篇原创文章 · 获赞 35 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/Li_Jiaqian/article/details/104232276