当前位置:网站首页>NowCoderTOP12-16——持续更新ing
NowCoderTOP12-16——持续更新ing
2022-07-25 10:20:00 【王嘻嘻-】
TOP12. 单链表的排序
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A4bAclx2-1658559347670)(D:\desktop\bite\mdpng\image-20220722200250502.png)]](/img/3f/4f9dd4c62ef3e3434bac91ad7f7936.png)
public class Solution {
/** * @param head ListNode类 the head node * @return ListNode类 */
public ListNode sortInList (ListNode head) {
// 法二: 转化为数组排序
ArrayList<Integer> nums = new ArrayList();
ListNode node = head;
// 遍历链表,将节点值加入数组
while(node != null) {
nums.add(node.val);
node = node.next;
}
node = head;
// 对数组排序
Collections.sort(nums);
// 遍历数组
for(int i = 0; i < nums.size() ; i++) {
// 将数组元素依次插入链表
node.val = nums.get(i);
node = node.next;
}
return head;
// 法一: 归并排序
// //链表为空或者只有一个元素,直接就是有序的
// if(head == null || head.next == null) return head;
// // 定义三个指针,快指针 fast 一次走两步,
// // 慢指针 mid 一次走一步,
// // 前驱指针 left 在慢指针前一个元素节点
// ListNode fast = head.next.next;
// ListNode mid = head.next;
// ListNode left = head;
// while(fast != null && fast.next != null) {
// fast = fast.next.next;
// mid = mid.next;
// left = left.next;
// }
// left.next = null;
// return Merge(sortInList(head),sortInList(mid));
// }
// // 合并两段有序链表
// public ListNode Merge(ListNode head1,ListNode head2) {
// // 边界条件,一个为空,返回另一个
// if(head1 == null) return head2;
// if(head2 == null) return head1;
// // 添加表头
// ListNode dummyHead = new ListNode(0);
// ListNode cur = dummyHead;
// // 两个链表都不为空,才可以进行元素的插入
// while(head1 != null && head2 != null) {
// if(head1.val <= head2.val) {
// cur.next = head1;
// head1 = head1.next;
// }else {
// cur.next = head2;
// head2 = head2.next;
// }
// cur = cur.next;
// }
// // 若两个链表长度不等,此时可能存在某一个链表为空的情况
// if(head1 == null) cur.next = head2;
// else cur.next = head1;
// return dummyHead.next;
}
}
TOP13. 判断一个链表是否为回文结构
public class Solution {
/** * @param head ListNode类 the head * @return bool布尔型 */
public boolean isPail (ListNode head) {
// 如果只有一个节点,返回 true
if(head.next == null) {
return true;
}
List<Integer> nums = new ArrayList<Integer>();
// 将链表转化为 list
while(head != null) {
nums.add(head.val);
head = head.next;
}
// 链表转化为 list 了
int i = 0;
int j = nums.size() - 1;
while(i < j) {
// 从前向后遍历,从后向前遍历,比较各个元素值是否相等
if(!nums.get(i).equals(nums.get(j))) return false;
++i;
--j;
}
return true;
}
}
TOP14. 链表的奇偶重排
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param head ListNode类 * @return ListNode类 */
public ListNode oddEvenList (ListNode head) {
if(head == null || head.next == null) return head;
// 偶节点
ListNode even = head.next;
// 奇节点
ListNode odd = head;
ListNode evenHead = even;
while(even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}
TOP15. 删除有序链表中重复的元素-I
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ovJLi9BK-1658559347671)(D:\desktop\bite\mdpng\image-20220723123822287.png)]](/img/84/a7fe35ea7d692dcb1241bcc9afe3a3.png)
public class Solution {
/** * @param head ListNode类 * @return ListNode类 */
public ListNode deleteDuplicates (ListNode head) {
if(head == null || head.next == null) return head;
ListNode cur = head;
while(cur.next != null) {
if(cur.val == cur.next.val) {
cur.next = cur.next.next;
}else {
cur = cur.next;
}
}
return head;
}
}
TOP16. 删除有序链表中所有重复的元素-II
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5g8fZTQj-1658559347671)(D:\desktop\bite\mdpng\image-20220723145319348.png)]](/img/54/b22635b533b888ce1581979dc56b3b.png)
public class Solution {
/** * @param head ListNode类 * @return ListNode类 */
public ListNode deleteDuplicates (ListNode head) {
if(head == null || head.next == null) return head;
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
while(cur.next != null && cur.next.next != null) {
if(cur.next.val == cur.next.next.val) {
int temp = cur.next.val;
while(cur.next != null && cur.next.val == temp){
cur.next = cur.next.next;
}
}else {
cur = cur.next;
}
}
return dummyHead.next;
}
}
边栏推荐
猜你喜欢

性能测试中TPS的计算【杭州多测师】【杭州多测师_王sir】

100W了!

HCIA experiment (06)

Reinforcement Learning 强化学习(三)

The University of Gottingen proposed clipseg: a model that can perform three segmentation tasks simultaneously using text and image prompts

mysql事务是什么

100W!

HCIP (01)

SQL语言(二)

Using numpy for elevation statistics and visualization
随机推荐
【flask高级】结合源码详解flask的运行机制(出入栈)
SQL语言(三)
最详细的mysql索引解析(文末附赠思维导图)
我,AI博士生,在线众筹研究主题
C3d model pytorch source code sentence by sentence analysis (III)
HCIA实验(10)NAT
JS hash table 01
Signal integrity (SI) power integrity (PI) learning notes (XXXIII) 102 general design rules to minimize signal integrity problems
信号完整性(SI)电源完整性(PI)学习笔记(三十三)102条使信号完整性问题最小化的通用设计规则
The practice of asynchronous servlet in image service
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
Introduction to onnx (open neural network exchange)
HCIA experiment (06)
mysql高级语句(一)(总有一个人的出现,让你的生活不再继续糟糕)
JDBC的APi补充
Analysis of event channel principle in Kraken
HCIP实验(01)
From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
SQL语言(二)
C3d model pytorch source code sentence by sentence analysis (I)