leetcode-cn | 链表

 复制带随机指针的链表

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

要求返回这个链表的深拷贝。 

输入:
{"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}

解释:
节点 1 的值是 1,它的下一个指针和随机指针都指向节点 2 。
节点 2 的值是 2,它的下一个指针指向 null,随机指针指向它自己。
 
提示:
你必须返回给定头的拷贝作为对克隆列表的引用。

因为题目要求是返回的是深拷贝,所以就是按照它原本的结构,一个节点一个节点的分配空间。

需要注意的几点是:(测试驱动的开发ORZ

  1. random有可能是null
  2. random有可能指向之前的节点
/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;

    Node() {}

    Node(int _val, Node* _next, Node* _random) {
        val = _val;
        next = _next;
        random = _random;
    }
};
*/
class Solution {
public:
    Node* copyRandomList(Node* head) {
        Node* res=new Node;
        res->next=NULL;
        Node* begin=head;
        Node* resbegin=res;
        while(begin!=NULL){
            Node* temp=new Node;
            temp->next=NULL;
            temp->val=begin->val;
            resbegin->next=temp;
            resbegin=temp;
            begin=begin->next;
        }
        
        begin=head;
        resbegin=res->next;
        while(begin!=NULL){
            Node* random=begin->random;
            if(random==NULL){
                resbegin->random=NULL;
            }
            else{
                Node* temp=head;   //random可能指向前方
                Node* restemp=res->next;
                while(random!=temp){
                    temp=temp->next;
                    restemp=restemp->next;
                }
                resbegin->random=restemp;
            }
            begin=begin->next;
            resbegin=resbegin->next;
        }
        return res->next;
    }
};

补充几个对链表的基本操作步骤:

参考链接:https://www.cnblogs.com/byonecry/p/4458821.html

节点的结构 
struct Node{
     int value;
     Node * next;
 };


双向链表的节点
struct DNode{
     int value;
     DNode * left;
     DNode * right;
 };

插入节点
//p节点后插入值为i的节点
void insertNode(Node *p, int i){
    Node* node = new Node;
    node->value = i;
    node->next = p->next;
    p->next = node;
}

删除节点
void deleteNode(Node *p){
    p->value = p->next->value;
    p->next = p->next->next;
}

环形链表

示例 1:

输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

输入:head = [1], pos = -1
输出:false
解释:链表中没有环。

# 一遍过,令人兴奋

/**
 * 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)
            return false;
        ListNode *one=head;
        ListNode *two=head;
        do{
            if(one->next==NULL||two->next==NULL||(two->next)->next==NULL)
                return false;
            one=one->next;
            two=(two->next)->next;
        }while(one!=two);
        return true;
    }
};

相交链表

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表

在节点 c1 开始相交。

 

示例 1:

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

 

示例 2:

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

 

示例 3:

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。

 

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
/**
 * 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) {
        int lena=getLen(headA);
        int lenb=getLen(headB);
        if(lena>lenb){
            int temp=lena-lenb;
            while(temp!=0){
                headA=headA->next;
                temp--;
            }
        }else{
            int temp=lenb-lena;
            while(temp!=0){
                headB=headB->next;
                temp--;
            }
        }
        if(headA==NULL||headB==NULL){
            return NULL;
        }
        else{
            while(headA!=headB||headA==NULL||headB==NULL){
                if(headA==NULL||headB==NULL){
                    return NULL;
                }
                else{
                    headA=headA->next;
                    headB=headB->next;
                }
            }
            return headA;
        }
    }
    
    int getLen(ListNode *head){
        int len=0;
        while(head!=NULL){
            len++;
            head=head->next;
        }
        return len;
    }
};

反转链表

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?

/**
 * 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) {
        if(head==NULL||head->next==NULL)
            return head;
        ListNode* nil=head;
        while(nil->next!=NULL){
            nil=nil->next;
        }
        while(head!=nil){
            ListNode* temp=head->next;
            head->next=nil->next;
            nil->next=head;
            head=temp;
        }
        return nil;
    }
};

在网上看到另一种解法,很巧妙了

/**
 * 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) {
        if(head==NULL||head->next==NULL)    return head;
        ListNode* left=NULL;
        ListNode* cur=head;
        ListNode* right=head;
        while(cur){
            right=cur->next;
            cur->next=left;
            left=cur;
            cur=right;
        }
        return left;
    }
};

回文链表

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false

示例 2:

输入: 1->2->2->1
输出: true

进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        //判断回文时主要需要解决的是链表不能随机访问
        if(head==NULL||head->next==NULL){
            return true;
        }
        vector<int> array;
        while(head!=NULL){
            array.push_back(head->val);
            head=head->next;
        }
        int left=0;
        int right=array.size()-1;
        while(left<=right){
            if(array[left]!=array[right])
                return false;
            else{
                left++;
                right--;
            }
        }
        return true;
    }
};

进阶自己没想出来,借助数组来完成的。


删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 -- head = [4,5,1,9],它可以表示为:

 

示例 1:

输入: head = [4,5,1,9], node = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:

输入: head = [4,5,1,9], node = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

 

说明:

  • 链表至少包含两个节点。
  • 链表中所有节点的值都是唯一的。
  • 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
  • 不要从你的函数中返回任何结果。

###这道题目真的是看了很久都没有理解是什么意思,没有给头结点,那要怎么删除??!!在看了其他的解答之后才明白,是删除给定的节点。

但是函数怎么调用啊,还是不明白orz

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val=(node->next)->val;
        node->next=(node->next)->next;
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值