• 力扣一.链表的运用


    一.合并生序链表

    将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

    示例 1:

    输入:l1 = [1,2,4], l2 = [1,3,4]
    输出:[1,1,2,3,4,4]
    输入:l1 = [], l2 = []
    输出:[]
    输入:l1 = [], l2 = [0]
    输出:[0]
    

    提示:

    • 两个链表的节点数目范围是 [0, 50]
    • -100 <= Node.val <= 100
    • l1 和 l2 均按 非递减顺序 排列

    【方法一】迭代

    1. public class Solution {
    2. public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
    3. ListNode prehead = new ListNode(-1);
    4. ListNode prev = prehead;
    5. while (l1 != null && l2 != null) {
    6. if (l1.val <= l2.val) {
    7. prev.next = l1;
    8. l1 = l1.next;
    9. } else {
    10. prev.next = l2;
    11. l2 = l2.next;
    12. }
    13. prev = prev.next;
    14. }
    15. // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
    16. prev.next = l1 == null ? l2 : l1;
    17. return prehead.next;
    18. }
    19. }

     【方法二】递归

    1. public class Solution {
    2. public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
    3. if (l1 == null) {
    4. return l2;
    5. } else if (l2 == null) {
    6. return l1;
    7. } else if (l1.val < l2.val) {
    8. l1.next = MergeTwoLists(l1.next, l2);
    9. return l1;
    10. } else {
    11. l2.next = MergeTwoLists(l1, l2.next);
    12. return l2;
    13. }
    14. }
    15. }

    【方法三】基本

    1. struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    2. if (l1==NULL)
    3. return l2;
    4. if (l2==NULL)
    5. return l1;
    6. struct ListNode* h = (struct ListNode*)malloc(sizeof(struct ListNode));
    7. struct ListNode *t = h;
    8. while (l1 && l2){
    9. if (l1->val <= l2->val){
    10. t->next = l1;
    11. l1 = l1->next;
    12. }
    13. else{
    14. t->next = l2;
    15. l2 = l2->next;
    16. }
    17. t = t->next;
    18. }
    19. if (l1==NULL) t->next = l2;
    20. else if (l2==NULL) t->next = l1;
    21. return h->next;
    22. }

    二.删除重复元素

    给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。

    示例 1:

    输入:head = [1,1,2]
    输出:[1,2]
    

    示例 2:

    输入:head = [1,1,2,3,3]
    输出:[1,2,3]
    

    提示:

    • 链表中节点数目在范围 [0, 300] 内
    • -100 <= Node.val <= 100
    • 题目数据保证链表已经按升序 排列
    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * struct ListNode *next;
    6. * };
    7. */
    8. struct ListNode* deleteDuplicates(struct ListNode* head){
    9. if(!head){
    10. return head;
    11. }
    12. struct ListNode* p=head;
    13. while(p->next){
    14. if(p->val==p->next->val){
    15. p->next=p->next->next;
    16. }
    17. else {
    18. p=p->next;
    19. }
    20. }
    21. return head;
    22. }

    三.环形链表

    给你一个链表的头节点 head ,判断链表中是否有环。

    如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。

    如果链表中存在环 ,则返回 true 。 否则,返回 false 。

    示例 1:

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

    示例 2:

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

    示例 3:

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

    提示:

    • 链表中节点的数目范围是 [0, 104]
    • -105 <= Node.val <= 105
    • pos 为 -1 或者链表中的一个 有效索引 。
      1. /**
      2. * Definition for singly-linked list.
      3. * struct ListNode {
      4. * int val;
      5. * struct ListNode *next;
      6. * };
      7. */
      8. bool hasCycle(struct ListNode *head) {
      9. if(head==NULL ||head->next==NULL){
      10. return false;
      11. }
      12. struct ListNode* slow=head;
      13. struct ListNode* fast=head->next;
      14. while(slow!=fast){
      15. if(fast==NULL||fast->next==NULL){
      16. return false;
      17. }
      18. slow=slow->next;
      19. fast=fast->next->next;
      20. }
      21. return true;
      22. }

  • 相关阅读:
    文档管理软件将办公室的业务模式转变为无纸化远程业务模式,提高员工生产力和保留率
    声明 Array List 的3种方式 ArrayList、Collection、List 的区别
    QQ第三方登录-python_web开发_django框架
    远程桌面连接下拉框IP地址删除
    Spark SQL简介
    企业数字化办公选SaaS服务还是私有化服务?
    vue的两种服务器端渲染方案
    预测和分类钻孔的毛刺钻孔切削ANN预测
    提高工作效率的神器:基于前端表格实现Chrome Excel扩展插件
    10.前端打包与nginx部署
  • 原文地址:https://blog.csdn.net/m0_74161592/article/details/133963891