【数据结构】链表经典题目

简介: 【数据结构】链表经典题目
移除链表元素

思路:先要判断这个链表是不是空链表,如果是空链表就直接返回NULL,就可以

代码:

struct ListNode* removeElements(struct ListNode* head, int val){
    if(head == NULL)
    {
        return NULL;
    }
    struct ListNode* cur = head;
    struct ListNode* newnode, *tail;
    newnode = tail = NULL;
    while(cur != NULL)
    {
        if(cur->val != val)
        {
            if(tail == NULL)
            {
                newnode = tail = cur;
            }
            else 
            {
                tail->next = cur;
                tail = tail->next;
            }
            cur = cur->next;
        }
        else
        {
            struct ListNode* next = cur->next;
            free(cur);
            cur = next;
        }
    }
    if(tail)
    {
        tail->next = NULL;
    }
    return newnode;
}

注意:这个题最后需要注意的是tail的下一个应该也是NULL,否则就有可能将最后一个链表连接。

合并两个有序链表

思路:重新用一个新的链表头来表示节点,如果小于的数就入到链表当中。

代码:

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1 == NULL) {
        return list2;
    }
    if(list2 == NULL) {
        return list1;
    }
    struct ListNode* head, *tail;
    head = tail = NULL;
    while(list1 && list2) {
        if(list1->val < list2->val) {
            if(tail == NULL) {
                head = tail = list1;
            }else {
                tail->next = list1;
                tail = tail->next;
            }
            list1 = list1->next;
        }else {
            if(tail == NULL) {
                head = tail = list2;
            }else {
                tail->next = list2;
                tail = tail->next;
            }
            list2 = list2->next;
        }
    }
    if(list1) {
        tail->next = list1;
    }
    if(list2) {
        tail->next = list2;
    }
    return head;
}
链表的中间节点

思路:这个题目只需要根据速度差,一个指针的速度是另一个指针速度的二倍,当快的指针到达终点的时候,慢指针刚好到达中间节点。

代码:

struct ListNode* middleNode(struct ListNode* head){
    struct ListNode *fast, *slow;
    fast = slow = head;
    while(fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}
链表中的倒数第k个结点

思路:先让快的链表走k步,然后两个指针一起往后移动,如果快的指针到达最后,慢的指针就是最后的结果。

代码:

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {
   // write code here
   struct ListNode *fast, *slow;
   fast = slow = pListHead;
   while(k) {
       if(fast == NULL) {
           return NULL;
       }
       fast = fast->next;
       k --;
   }
   while(fast) {
       fast = fast->next;
       slow = slow->next;
   }
   return slow;
}
反转链表

代码:

struct ListNode* reverseList(struct ListNode* head){
    if(head == NULL) {
        return NULL;
    }
    struct ListNode* n1, *n2, *n3;
    n1 = NULL;
    n2 = head;
    n3 = n2->next;
    while(n2) {
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if(n3) {
            n3 = n3->next;
        }
    }
    return n1;
}
链表分割

对于最后一个数字,还有不同的情况考虑:

代码:

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        // write code here
        struct ListNode* lessHead, *lessTail, *greaterHead, *greaterTail;
        lessHead = lessTail = (struct ListNode*)malloc(sizeof(struct ListNode));
        greaterHead = greaterTail = (struct ListNode*)malloc(sizeof(struct ListNode));
        lessTail->next = greaterTail->next = NULL;
        struct ListNode* cur = pHead;
        while(cur) {
            if(cur->val < x) {
                lessTail->next = cur;
                lessTail = lessTail->next;
            }else {
                greaterTail->next = cur;
                greaterTail = greaterTail->next;
            }
            cur = cur->next;
        }
        lessTail->next = greaterHead->next;
        greaterTail->next = NULL;
        pHead = lessHead->next;
        free(lessHead);
        free(greaterHead);
        return pHead;
    }
};
链表的回文结构

思路:直接找到中间节点,然后反转中间节点到最后的链表就可以。

代码:

struct ListNode* findMid(struct ListNode* A) {
    struct ListNode* slow = A, *fast = A;
    while(fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}
struct ListNode* reverse(struct ListNode* mid) {
    struct ListNode* n1 = NULL, *n2 = mid, *n3 = n2->next;
    while(n2) {
        n2->next = n1;
        n1 = n2;
        n2 = n3;
        if(n3) {
            n3 = n3->next;
        }
    }
    return n1;
}
class PalindromeList {
public:
    bool chkPalindrome(ListNode* A) {
        // write code here
        struct ListNode* mid = findMid(A);
        struct ListNode* rhead = reverse(mid);
        while(A && rhead) {
            if(A->val != rhead->val) {
                return false;
            }
            A = A->next;
            rhead = rhead->next;
        }
        return true;
    }
};
相交链表

思路:找到两个其中长的一个链表,然后走差值步,一起走看是否有相等的地址。

代码:

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
        struct ListNode* curA = headA, *curB = headB;
        int lenA = 0, lenB = 0;
      //分别找到链表A和链表B的长度
        while(curA) {
            lenA ++;
            curA = curA->next;
        }
        while(curB) {
            lenB ++;
            curB = curB->next;
        }
      //如果最后不相等,则绝对不存在公共节点
      //如果相等,则一定存在公共节点
        if(curA != curB) {
            return NULL;
        }
        int gap = abs(lenA - lenB);
      //假设A>B
        struct ListNode* LongList = headA, *ShortList = headB;
        if(lenB > lenA) {
            LongList = headB;
            ShortList = headA;
        }
        while(gap --) {
            LongList = LongList->next;
        }
        while(LongList != ShortList) {
            LongList = LongList->next;
            ShortList = ShortList->next;
        }
        return LongList;
}
环形链表

思路:两个指针,一个指针每次跑一步,另一个指针每次跑两步,如果有环两个指针肯定能够相遇。

代码:

bool hasCycle(struct ListNode *head) {
    struct ListNode* slow = head, *fast = head;
    while(fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast) {
            return true;
        }
    }
    return false;
}

环形链表II(找到如环的节点)

结论:让一个指针从链表的起始位置开始遍历链表,同时让一个指针从判环相遇点的位置开始绕环运行,两个指针都是每次均走一步,最终肯定会在入口点的位置相遇。

代码:

struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode* slow = head, *fast = head;
    while(fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast) {
            struct ListNode* meet = slow;
            while(head != meet) {
                head = head->next;
                meet = meet->next;
            }
            return head;
        }
    }
    return NULL;
}
复制带随机指针的链表

思路:

  1. 拷贝节点在源节点的后面

代码实现:

struct Node* copyRandomList(struct Node* head) {
  struct Node* cur = head;
    while(cur) {
        struct Node* next = cur->next;
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        cur->next = copy;
        copy->next = next;
        cur = next;
    }
}
  1. 设置拷贝节点的random

代码实现:

struct Node* copyRandomList(struct Node* head) {
  struct Node* cur = head;
    while(cur) {
        struct Node* next = cur->next;
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        cur->next = copy;
        copy->next = next;
        cur = next;
    }
    //设置拷贝节点的random
    cur = head;
    while(cur) {
        struct Node* copy = cur->next;
        if(cur->next == NULL) {
            copy->random = NULL;
        }else {
            copy->random = cur->random->next;
        }
        cur = cur->next->next;
    }
}
  1. 拷贝节点解下来,连接组成拷贝链表

代码实现:

struct Node* copyRandomList(struct Node* head) {
  struct Node* cur = head;
    while(cur) {
        struct Node* next = cur->next;
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        cur->next = copy;
        copy->next = next;
        cur = next;
    }
    cur = head;
    while(cur) {
        struct Node* copy = cur->next;
        if(cur->random == NULL) {
            copy->random = NULL;
        }else {
            copy->random = cur->random->next;
        }
        cur = cur->next->next;
    }
    cur = head;
    struct Node* copyHead = NULL, *copyTail = NULL;
    while(cur) {
        struct Node* copy = cur->next;
        struct Node* next = copy->next;
        cur->next = next;
        if(copyTail == NULL) {
            copyHead = copyTail = copy;
        }else {
            copyTail->next = copy;
            copyTail = copyTail->next;
        }
        cur = next;
    }
    return copyHead;
}
相关文章
|
5天前
|
Java
java数据结构,双向链表的实现
文章介绍了双向链表的实现,包括数据结构定义、插入和删除操作的代码实现,以及双向链表的其他操作方法,并提供了完整的Java代码实现。
java数据结构,双向链表的实现
|
27天前
|
存储 Java 索引
【数据结构】链表从实现到应用,保姆级攻略
本文详细介绍了链表这一重要数据结构。链表与数组不同,其元素在内存中非连续分布,通过指针连接。Java中链表常用于需动态添加或删除元素的场景。文章首先解释了单向链表的基本概念,包括节点定义及各种操作如插入、删除等的实现方法。随后介绍了双向链表,说明了其拥有前后两个指针的特点,并展示了相关操作的代码实现。最后,对比了ArrayList与LinkedList的不同之处,包括它们底层实现、时间复杂度以及适用场景等方面。
42 10
【数据结构】链表从实现到应用,保姆级攻略
|
2月前
|
存储 C语言
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
【数据结构】c语言链表的创建插入、删除、查询、元素翻倍
|
2月前
|
存储 Java 程序员
"揭秘HashMap底层实现:从数组到链表,再到红黑树,掌握高效数据结构的秘密武器!"
【8月更文挑战第21天】HashMap是Java中重要的数据结构,采用数组+链表/红黑树实现,确保高效查询与更新。构造方法初始化数组,默认容量16,负载因子0.75触发扩容。`put`操作通过计算`hashCode`定位元素,利用链表或红黑树处理冲突。`get`和`remove`操作类似地定位并返回或移除元素。JDK 1.8优化了链表转红黑树机制,提升性能。理解这些原理能帮助我们更高效地应用HashMap。
34 0
|
2月前
|
存储 算法
【初阶数据结构篇】顺序表和链表算法题
此题可以先找到中间节点,然后把后半部分逆置,最近前后两部分一一比对,如果节点的值全部相同,则即为回文。
|
2月前
|
存储 测试技术
【初阶数据结构篇】双向链表的实现(赋源码)
因为头结点的存在,plist指针始终指向头结点,不会改变。
|
2月前
|
存储 测试技术
【初阶数据结构篇】单链表的实现(附源码)
在尾插/尾删中,都需要依据链表是否为空/链表是否多于一个节点来分情况讨论,目的是避免对空指针进行解引用造成的错误。
|
2月前
|
算法
【数据结构与算法】共享双向链表
【数据结构与算法】共享双向链表
13 0
|
2月前
|
算法
【数据结构与算法】双向链表
【数据结构与算法】双向链表
13 0
|
2月前
|
算法
【数据结构与算法】循环链表
【数据结构与算法】循环链表
13 0