剑指 Offer 35. 复杂链表的复制

本题重点在于怎么个复制法(包含random指针)。

 两种方法:1、哈希表

                   2、拼接与拆分

1、哈希表

特点在于将节点保存,取节点及链接时方便。

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        Map<Node, Node> map = new HashMap<>();
        // 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
        while(cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        // 4. 构建新链表的 next 和 random 指向
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        // 5. 返回新链表的头节点
        return map.get(head);
    }
}

2、 拼接与拆分

在原节点后复制创建新的节点,在通过 cur.next.random = cur.random.next 解决random指针问题。

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        // 1. 复制各节点,并构建拼接链表
        while(cur != null) {
            Node tmp = new Node(cur.val);
            tmp.next = cur.next;
            cur.next = tmp;
            cur = tmp.next;
        }
        // 2. 构建各新节点的 random 指向
        cur = head;
        while(cur != null) {
            if(cur.random != null)
                cur.next.random = cur.random.next;
            cur = cur.next.next;
        }
        // 3. 拆分两链表
        cur = head.next;
        Node pre = head, res = head.next;
        while(cur.next != null) {
            pre.next = pre.next.next;
            cur.next = cur.next.next;
            pre = pre.next;
            cur = cur.next;
        }
        pre.next = null; // 单独处理原链表尾节点
        return res;      // 返回新链表头节点
    }
}

  • 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、付费专栏及课程。

余额充值