剑指offer35.复杂链表的复制

题目描述

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

解析
思路
  • 刚开始不明白题目的意思,翻了翻评论,是需要对此链表进行deep copy(简单来说,就是开辟独立的内存空间,对原链表进行复制。这样原链表改变时,并不会影响复制后的链表。就像画板上画了一个苹果,浅拷贝就是告诉你画板上的某个位置有个苹果,而深拷贝是在画板上重新画了一个一模一样的苹果,原来的苹果被擦掉并不影响深拷贝的苹果)。
  • 继续分析题意,需要根据原链表节点node的val值构造一个新的节点copynode,同时又需要把原节点node的next、random指针所指向的节点告诉copynode,但又不能指向原来链表上的节点。这里主要的思维断点在于为何使用HashMap的数据结构。仔细思考。
  • 代码思路:将原链表节点复制,以<原节点,新节点><K,V>形式存储,因此:根据原节点的next、random指针的key值(即next、random节点)get到value值(即新节点的next、random节点),赋给以原节点为key的value值(即新节点)的next、random节点。(说的有点绕,直接看代码很好理解)
public Node copyRandomList(Node head) {
      HashMap<Node, Node> map = new HashMap<>();
      Node cur=head;
      while(cur!=null) {
    	  Node copynode = new Node(cur.val);
    	  map.put(cur, copynode);
    	  cur=cur.next;
      }
      cur=head;
      while(cur!=null) {
    	  map.get(cur).next=map.get(cur.next);
    	  map.get(cur).random=map.get(cur.random);
    	  cur=cur.next;
      }
      return map.get(head);       
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于C语言实现的单链表代码: ```c #include <stdio.h> #include <stdlib.h> //定义链表节点 typedef struct Node { int data; struct Node* next; } Node; //初始化链表 Node* initList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } //清空链表 void clearList(Node* head) { Node* p, * q; p = head->next; while (p) { q = p->next; free(p); p = q; } head->next = NULL; } //求链表长度 int getLength(Node* head) { int len = 0; Node* p = head->next; while (p) { len++; p = p->next; } return len; } //遍历链表 void traverseList(Node* head) { Node* p = head->next; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } //查找元素位置 int findElement(Node* head, int value) { int pos = 0; Node* p = head->next; while (p) { pos++; if (p->data == value) { return pos; } p = p->next; } return -1; } //插入元素 void insertElement(Node* head, int value, int pos) { Node* p = head; Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; while (pos > 0 && p) { pos--; if (pos == 0) { newNode->next = p->next; p->next = newNode; } p = p->next; } } //删除元素 void deleteElement(Node* head, int value) { Node* p = head; Node* q; while (p->next) { if (p->next->data == value) { q = p->next; p->next = q->next; free(q); return; } p = p->next; } } int main() { Node* head = initList(); insertElement(head, 1, 1); insertElement(head, 2, 2); insertElement(head, 3, 3); insertElement(head, 4, 4); insertElement(head, 5, 5); printf("链表元素为:"); traverseList(head); printf("链表长度为:%d\n", getLength(head)); int pos = findElement(head, 3); if (pos == -1) { printf("元素不存在!\n"); } else { printf("元素3在链表中的位置为:%d\n", pos); } deleteElement(head, 4); printf("删除元素4后,链表元素为:"); traverseList(head); clearList(head); printf("清空链表后,链表元素为:"); traverseList(head); return 0; } ``` 这里的代码实现了单链表的初始化、清空、求链表长度、遍历、查找、插入和删除操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值