牛客top100 - 自刷打卡day1 - 链表

news/2024/11/15 4:00:57/

top100-打卡day1

    • 链表
      • BM1反转链表
      • BM2链表内指定区间反转
      • BM3链表中的节点每k个一组翻转
      • BM4合并两个排序的链表
      • BM5合并k个已排序的链表
      • BM6判断链表中是否有环
      • BM7链表中环的入口结点
      • BM8链表中倒数最后k个结点
      • BM9删除链表的倒数第n个节点
      • BM10两个链表的第一个公共结点
      • BM11链表相加(二)
      • BM12单链表的排序
      • BM13判断一个链表是否为回文结构
      • BM14链表的奇偶重排
      • BM15 删除有序链表中重复的元素-I
      • BM16 删除有序链表中重复的元素-II

链表

BM1反转链表

反转链表_牛客题霸_牛客网 (nowcoder.com)

/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Solution {public ListNode ReverseList(ListNode head) {if (head == null)return head;ListNode pre = null;ListNode cur = head;while (cur !=  null && cur.next != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}cur.next = pre;return cur;}}

BM2链表内指定区间反转

链表内指定区间反转_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head ListNode类* @param m int整型* @param n int整型* @return ListNode类*///        p   c   n//    2       4//1-->2-->3-->4-->5-->nullpublic ListNode reverseBetween (ListNode head, int m, int n) {int num = n - m;//2if (num <= 0 ) return head;ListNode dummy = new ListNode(-1);dummy.next = head;ListNode pre = dummy;while ((--m) != 0) {pre = pre.next;}ListNode lHead = pre;ListNode lCur = pre.next;ListNode cur = pre.next;while (num != 0) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;num--;}ListNode next = cur.next;cur.next = pre;lHead.next = cur;lCur.next = next;return dummy.next;}
}

BM3链表中的节点每k个一组翻转

链表中的节点每k个一组翻转_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head ListNode类* @param k int整型* @return ListNode类*/public ListNode reverseKGroup (ListNode head, int k) {int m = k;// write code hereif (head == null) return head;ListNode pHead = head;for (int i = 0; i < k; i++) {if (pHead == null) {return head;}pHead = pHead.next;}ListNode pre = null;ListNode cur = head;while((m--) != 0) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}head.next = reverseKGroup(pHead, k);return pre;}
}

BM4合并两个排序的链表

合并两个排序的链表_牛客题霸_牛客网 (nowcoder.com)

/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Solution {public ListNode Merge(ListNode list1, ListNode list2) {ListNode l1 = list1;ListNode l2 = list2;ListNode dummy = new ListNode(-1);ListNode cur = dummy;while (l1 != null && l2 != null) {if (l1.val >= l2.val) {cur.next = l2;l2 = l2.next;cur = cur.next;} else {cur.next = l1;l1 = l1.next;cur = cur.next;}}if (l1 != null) {cur.next = l1;}if (l2 != null) {cur.next = l2;}return dummy.next;}
}

BM5合并k个已排序的链表

合并k个已排序的链表_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode Merge(ListNode list1, ListNode list2) {ListNode l1 = list1;ListNode l2 = list2;ListNode dummy = new ListNode(-1);ListNode cur = dummy;while (l1 != null && l2 != null) {if (l1.val >= l2.val) {cur.next = l2;l2 = l2.next;cur = cur.next;} else {cur.next = l1;l1 = l1.next;cur = cur.next;}}if (l1 != null) {cur.next = l1;}if (l2 != null) {cur.next = l2;}return dummy.next;}public ListNode mergeKLists(ArrayList<ListNode> lists) {return divideMerge(lists, 0, lists.size() -1);}ListNode divideMerge(ArrayList<ListNode> lists, int left, int right){if(left > right ){return null;}if(left == right) {return lists.get(left);}int mid = left + right >> 1;return Merge(divideMerge(lists, left, mid), divideMerge(lists, mid + 1, right));}
}

BM6判断链表中是否有环

判断链表中是否有环_牛客题霸_牛客网 (nowcoder.com)

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {ListNode fast = head;ListNode slow = head;while(fast != null && slow != null) {if(fast.next != null) {fast = fast.next.next;}else{return false;}slow = slow.next;if(slow == fast) {return true;}}return false;}
}

BM7链表中环的入口结点

链表中环的入口结点_牛客题霸_牛客网 (nowcoder.com)

/*public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}
*/
public class Solution {public ListNode EntryNodeOfLoop(ListNode pHead) {if(pHead.next == null) return null;ListNode fast = pHead;ListNode slow = pHead;while(fast != null && slow != null) {if(fast.next != null){fast = fast.next.next;}else{return null;}slow = slow.next;if(fast == slow) {break;}}if(fast == null || slow == null) return null;fast = pHead;while(fast != slow){fast = fast.next;slow = slow.next;}return slow;}
}

BM8链表中倒数最后k个结点

链表中倒数最后k个结点_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;*   public ListNode(int val) {*     this.val = val;*   }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param pHead ListNode类* @param k int整型* @return ListNode类*/public ListNode FindKthToTail (ListNode pHead, int k) {// write code hereListNode pre = pHead;ListNode cur = pHead;int tk = k;while (pre != null && (tk--) != 0) {pre = pre.next;}if(pre == null && tk > 0) return null;while(pre != null) {pre = pre.next;cur = cur.next;}return cur;}
}

BM9删除链表的倒数第n个节点

删除链表的倒数第n个节点_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head ListNode类* @param n int整型* @return ListNode类*/public ListNode removeNthFromEnd (ListNode head, int n) {// write code hereint tn = n;ListNode cur = new ListNode(-1);cur.next = head;ListNode pre = head;ListNode red = cur;while (pre != null && (--tn) != 0) {pre = pre.next;}if (pre != null && tn > 0) return null;while (pre.next != null) {pre = pre.next;cur = cur.next;}cur.next = cur.next.next;return red.next;}
}

BM10两个链表的第一个公共结点

两个链表的第一个公共结点_牛客题霸_牛客网 (nowcoder.com)

/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Solution {public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {ListNode p1 = pHead1;ListNode p2 = pHead2;while(p1 != p2){p1 = p1 == null ? pHead2 : p1.next;p2 = p2 == null ? pHead1 : p2.next;}return p1;}
}

BM11链表相加(二)

链表相加(二)_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head1 ListNode类* @param head2 ListNode类* @return ListNode类*/public ListNode reverseList(ListNode head) {if (head == null) return head;ListNode pre = null;ListNode cur = head;while (cur != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}public ListNode addInList (ListNode head1, ListNode head2) {// write code herehead1 = reverseList(head1);head2 = reverseList(head2);ListNode h1 = head1;ListNode h2 = head2;int pVal = 0;ListNode res = new ListNode(-1);ListNode rHead = res;while (h1 != null && h2 != null) {int val = h1.val + h2.val + pVal;pVal = val >= 10 ? val / 10 : 0;val = val % 10;res.next = new ListNode(val);res = res.next;h1 = h1.next;h2 = h2.next;}while (h1 != null) {int val = pVal + h1.val;pVal = val >= 10 ? val / 10 : 0;val = val % 10;res.next = new ListNode(val);res = res.next;h1 = h1.next;}while (h2 != null) {int val = pVal + h2.val;pVal = val >= 10 ? val / 10 : 0;val = val % 10;res.next = new ListNode(val);res = res.next;h2 = h2.next;}if(pVal != 0) {res.next = new ListNode(pVal);}return reverseList(rHead.next);}
}

BM12单链表的排序

单链表的排序_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head ListNode类 the head node* @return ListNode类*/public ListNode sortInList (ListNode head) {// write code herePriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {public int compare(ListNode o1, ListNode o2) {return o1.val - o2.val;}});ListNode cur = head;while (cur != null) {queue.offer(cur);cur = cur.next;}ListNode res = new ListNode(-1);ListNode rHead = res;while (!queue.isEmpty()) {res.next = queue.poll();res = res.next;}res.next = null;return rHead.next;}
}

BM13判断一个链表是否为回文结构

判断一个链表是否为回文结构_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/*** * @param head ListNode类 the head* @return bool布尔型*/public ListNode reverseList(ListNode head){ListNode pre = null;ListNode cur = head;while(cur != null) {ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}public boolean isPail (ListNode head) {if(head == null || head.next == null) return true;// write code hereListNode fast = head;ListNode slow = head;while(fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;}ListNode rHead = reverseList(slow);ListNode lHead = head;while(lHead != null && rHead != null) {if(lHead.val != rHead.val){return false;}lHead = lHead.next;rHead = rHead.next;}return true;}
}

BM14链表的奇偶重排

链表的奇偶重排_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;*   public ListNode(int val) {*     this.val = val;*   }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可* * @param head ListNode类 * @return ListNode类*/public ListNode oddEvenList (ListNode head) {// write code hereListNode J = new ListNode(-1);ListNode O = new ListNode(-1);ListNode cj = J;ListNode co = O;ListNode cur = head;boolean flag = false;while(cur != null) {if(flag) {co.next = cur;co = co.next;flag = ! flag;}else{cj.next = cur;cj = cj.next;flag = ! flag;}cur = cur.next;}co.next = null;cj.next = O.next;return J.next;}
}

BM15 删除有序链表中重复的元素-I

删除有序链表中重复的元素-I_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/*** * @param head ListNode类 * @return ListNode类*/public ListNode deleteDuplicates (ListNode head) {if(head == null) return head;// write code hereListNode dummy = new ListNode(-1);dummy.next = head; ListNode cur = head;ListNode next = cur.next;if(next == null) return head;while(next != null){if(cur.val == next.val){cur.next = next.next;}else{cur = next;}next = next.next;} return dummy.next;}
}

BM16 删除有序链表中重复的元素-II

删除有序链表中重复的元素-II_牛客题霸_牛客网 (nowcoder.com)

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {/**** @param head ListNode类* @return ListNode类*/public ListNode deleteDuplicates (ListNode head) {// write code hereif (head == null) return null;ListNode pre = new ListNode(-1);pre.next = head;ListNode resHead = pre;ListNode cur = head;ListNode next = cur.next;if (next == null) return head;while (next != null) {if (cur.val == next.val) {while (next != null && cur.val == next.val) {next = next.next;}cur = next;if (next != null) {next = next.next;}pre.next = cur;} else {pre = cur;cur = next;next = next.next;}}return resHead.next;}
}

http://www.ppmy.cn/news/33281.html

相关文章

CSDN周赛第37期题解(Python版)

这期周赛题目和测试集还算完整&#xff0c;没有出现往期的bug。1、题目名称&#xff1a;幼稚班作业幼稚园终于又有新的作业了。 老师安排同学用发给同学的4根木棒拼接成一个三角形。 当然按照正常的逻辑&#xff0c;如果不能拼接成三角形。 必然要折断某个木棍来拼接三角形。 可…

【Linux】进程的程序替换

文章目录1. 程序替换1.创建子进程的目的是什么&#xff1f;2.了解程序是如何进行替换的3. 程序替换的基本原理当创建进程的时候&#xff0c;先有进程数据结构&#xff0c;还是先加载代码和数据&#xff1f;程序替换是整体替换&#xff0c;不是局部替换execl 返回值4. 替换函数1…

vue2+高德地图web端开发使用

创建vue2项目我们创建一个vue2项目&#xff0c;创建vue2项目就不用再多说了吧&#xff0c;使用“vue create 项目名 ”创建即可注册高德地图高德地图官网地址&#xff1a;https://lbs.amap.com/如果是第一次使用&#xff0c;点击注册然后进入我们的控制台注册完之后进入控制台&…

ES6新特性--变量声明

可以使用let关键字来声明变量let a;let b,c;//同时声明多个变量let stu = 张三;let name =李四,age = 12;//声明变量的同时赋值 let关键字使用的注意事项(1).变量在声明的时候不可以重复,这也符合其他语言的变量声明规范 let name = 李四; let name = 张三;//这里开始报错,但…

2023年Android现代开发

2023年现代Android开发 下面与大家分享如何构建具有2023年最新趋势的Android应用程序。 Android是什么&#xff1f; Android 是一种基于 Linux 内核并由 Google 开发的开源操作系统。它用于各种设备&#xff0c;包括智能手机、平板电脑、电视和智能手表。 目前&#xff0c…

IntelliJIDEA 常用快捷键

IntelliJIDEA 常用快捷键 Alt Enter 导入包&#xff0c;自动修正&#xff0c;自动创建变量名。 Ctrl Alt O 优化导入的类和包 Ctrl / 单行注释 (//) Ctrl Shift / 多行注释 (/* … */) 方法或类说明注释&#xff08;文档注释&#xff09; 在一个方法或类的开头&#xf…

STM32 OTA应用开发——通过内置DFU实现USB升级(方式1)

STM32 OTA应用开发——通过内置DFU实现USB升级&#xff08;方式1&#xff09; 目录STM32 OTA应用开发——通过内置DFU实现USB升级&#xff08;方式1&#xff09;前言1 硬件介绍2 环境搭建2.1 Keil uVsion2.2 zadig2.3 STM32CubeProgrammer2.4 安装USB驱动3 OTA升级结束语前言 …

云上办公系统项目

云上办公系统项目1、云上办公系统1.1、介绍1.2、核心技术1.3、开发环境说明1.4、产品展示后台前台1.5、 个人总结2、后端环境搭建2.1、建库建表2.2、创建Maven项目pom文件guigu-oa-parentcommoncommon-utilservice-utilmodelservice-oa配置数据源、服务器端口号application.yml…