7.8~7.10练习

目录

1.扑克牌游戏

2.链表基本功能的实现(单项链表)

 3.移除链表元素力扣

4.反转链表力扣

5.链表的中间结点

5.返回倒数第k个节点​编辑

6.合并两个有序链表

 7.链表基本功能的实现(双向链表)

8.链表分割


1.扑克牌游戏


public class Card {private String suit;private int rank;public Card( int rank,String suit) {this.suit = suit;this.rank = rank;}@Overridepublic String toString() {return"{"+suit+rank+"} ";}
}

import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class CardDemo {public static final String[] stuits={"♠","方块","♥","♣"};public List<Card> buyCard() {List<Card> cardList = new ArrayList<>();for (int i = 0; i < 13; i++) {for (int j = 0; j < 4; j++) {int rank = i;String suit = stuits[j];Card card = new Card(rank,suit);cardList.add(card);}}return cardList;}public void shuffle(List<Card> cardList){Random random = new Random();for (int i =cardList.size()-1; i > 0; i--) {int index = random.nextInt(i);swap(cardList,i,index);}}private void swap (List<Card>cardList,int i,int j){Card tmp = cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,tmp);}public List<List<Card>> play(List<Card>cardList){List<Card> hand0 = new ArrayList<>();List<Card> hand1 = new ArrayList<>();List<Card> hand2 = new ArrayList<>();List<List<Card>> hand = new ArrayList<>();hand.add(hand0);hand.add(hand1);hand.add(hand2);for (int i = 0; i < 5; i++) {for (int j = 0; j < 3; j++) {Card card = cardList.remove(0);hand.get(j).add(card);}}return hand;}
}
import java.util.List;public class Test {public static void main(String[] args) {//买牌CardDemo cardDemo = new CardDemo();List<Card> cardList = cardDemo.buyCard();System.out.println(cardList);//洗牌cardDemo.shuffle(cardList);//轮流发牌List <List<Card>> ret = cardDemo.play(cardList);for (int i = 0; i < ret.size(); i++) {System.out.println("第"+(i+1)+"个人的牌"+ret.get(i));}}
}

2.链表基本功能的实现(单项链表)

public class Test {public static void main(String[] args) {MySingleList list = new MySingleList();list.CreatList();list.clear();list.display();}
}

public class MySingleList implements IList {static  class ListNode{private int val;private ListNode next;public   ListNode(int val){this.val = val;}}public ListNode head;public void CreatList(){ListNode node1 = new ListNode(12);ListNode node2 = new ListNode(12);ListNode node3 = new ListNode(23);ListNode node4 = new ListNode(45);ListNode node5 = new ListNode(56);node1.next = node2;node2.next = node3;node3.next = node4;node4.next = node5;this.head = node1;}@Overridepublic void addFirst(int data) {ListNode node = new ListNode(data);node.next = head;head = node;}@Overridepublic void addLast(int data) {ListNode node = new ListNode(data);ListNode cur = head;if(head == null){head = node;return;}while(cur.next != null ){cur = cur.next;} cur.next = node;}@Overridepublic void addIndex(int index, int data) {int len = size();if(index < 0 || index > len){System.out.println("index位置不合法");return;}if( index == 0){addFirst(data);return;}if(index == len){addLast(data);return;}ListNode node = new ListNode(data);ListNode cur =head;while(index - 1 != 0){cur = cur.next;index--;}node.next = cur.next;cur.next = node;}@Overridepublic boolean contains(int key) {ListNode cur = head;while(cur != null){if(cur.val == key){return true;}elsecur = cur.next;}return false;}@Overridepublic void remove(int key) {if(head == null){return;}if(head.val == key){head = head.next;return;}ListNode cur = findNodeOfKey(key);if(cur == null){return;}cur.next = cur.next.next;}private ListNode findNodeOfKey(int key){ListNode cur = head;while(cur.next != null){if(cur.next.val == key){return cur;}cur = cur.next;}return null;}@Overridepublic void removeAllKey(int key) {if (head == null) {return;}ListNode pre = head;ListNode cur = head.next;while (cur != null) {if (cur.val == key) {pre.next = cur.next;cur = cur.next;} else {pre = cur;cur = cur.next;}}if (head.val == key){head = head.next;}}@Overridepublic int size() {ListNode cur = this.head;int len = 0;while(cur != null){len++;cur = cur.next;}System.out.println();return len;}@Overridepublic void clear() {ListNode cur = head;while(cur != null){ListNode curN = cur.next;cur.next = null;cur = curN;}head = null;}@Overridepublic void display() {ListNode cur = head;while ((cur != null)){System.out.print(cur.val+" ");cur = cur.next;}}
}
public interface IList {public void addFirst(int data);public void addLast(int data);public void addIndex(int index,int data);public boolean contains (int key);public void remove(int key);public void removeAllKey(int key);public int size();public void clear();public void display();}

 3.移除链表元素力扣

public class Solution {public ListNode removeElements(ListNode head, int val) {if(head == null){return head;}ListNode pre = head;ListNode cur = head.next;while(cur != null){if(cur.val == val){pre.next = cur.next;cur = cur.next;}else{pre = cur;cur = cur.next;}}if(head.val == val){head = head.next;}return head;}}

4.反转链表力扣

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {if(head == null){return head;}ListNode cur = head.next;head.next = null;while(cur != null){ListNode curN = cur.next;cur.next = head;head = cur;cur = curN;}return head;}
}

5.链表的中间结点

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode middleNode(ListNode head) {if(head == null){return head;}ListNode fast = head;ListNode slow = head;while(fast != null && fast.next != null){fast = fast.next.next;slow = slow.next;}return slow;}
}

5.返回倒数第k个节点

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) { val = x; }* }*/
class Solution {public int kthToLast(ListNode head, int k) {if(head == null){return head.val;}ListNode fast = head;ListNode slow = head;int count = k - 1;while(count != 0){fast = fast.next;count--;}while(fast.next != null){fast = fast.next;slow = slow.next;}return slow.val;}
}

6.合并两个有序链表

 /*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode newHead = new ListNode(-1);ListNode tmp = newHead;while(list1 != null && list2 != null){if(list1.val <= list2.val){tmp.next = list1;list1 = list1.next;tmp = tmp.next;}else{tmp.next = list2;tmp = tmp.next;list2 = list2.next;}}if(list1 != null){tmp.next = list1;}if(list2 != null){tmp.next = list2;}return newHead.next;}
}

 7.链表基本功能的实现(双向链表)

public class test {public static MyLinkedList.ListNode  mergeTwoLists(MyLinkedList.ListNode list1,MyLinkedList. ListNode list2) {MyLinkedList. ListNode newHead = new MyLinkedList.ListNode(-1);MyLinkedList. ListNode tmp = newHead;while(list1 != null && list2 != null){if(list1.val < list2.val){tmp.next = list1;list1 = list1.next;tmp = tmp.next;}else{tmp.next = list2;tmp = tmp.next;list2 = list2.next;}}if(list1 != null){tmp.next = list1;}if(list2 != null){tmp.next = list2;}return newHead.next;}public static void main(String[] args) {MyLinkedList myLinkedList = new MyLinkedList();myLinkedList.addLast(1);myLinkedList.addLast(5);myLinkedList.addLast(9);myLinkedList.addLast(21);myLinkedList.addLast(7);myLinkedList.addLast(21);MyLinkedList myLinkedList1 =new MyLinkedList();myLinkedList1.addLast(1);myLinkedList1.addLast(5);myLinkedList1.addLast(9);myLinkedList1.addLast(21);myLinkedList1.addLast(7);myLinkedList1.addLast(21);MyLinkedList.ListNode newHead =  mergeTwoLists( myLinkedList.head,myLinkedList1.head);myLinkedList.display2(newHead);}
}
public class MyLinkedList implements IList{static class ListNode {public int val;public ListNode preV;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;public ListNode last;@Overridepublic void addFirst(int data) {ListNode cur = head;ListNode node = new ListNode(data);if(head.next == null){head = last = node;}else{node.next = cur;cur.preV = node;head = node;}}@Overridepublic void addLast(int data) {ListNode cur = head;ListNode node = new ListNode(data);if(head == null) {head = last = node;}else{last.next = node;node.preV = last;last = node;}}@Override//在指点位置增删元素public void addIndex(int index, int data) {int len = size();if(index < 0 || index > len ){System.out.println("不符合条件");return;}if(index == 0){addFirst(index);return;}if(index == len){addLast(data);return;}else{ListNode cur = findOfKey(index);ListNode node = new ListNode(data);node.next = cur;node.preV = cur.preV;cur.preV.next = node;cur.preV = node;}}private ListNode findOfKey(int index){ListNode cur = head;while(index != 0){cur = cur.next;index--;}return cur;}@Overridepublic boolean contains(int key) {ListNode cur = head;while(cur != null){if(cur.val == key){return true;}cur = cur.next;}return false;}@Overridepublic void remove(int key) {ListNode cur = head;while(cur != null) {if (cur.val == key) {if (cur == head) {head = head.next;if (head != null) {head.preV = null;}} else {cur.preV.next = cur.next;if ( cur.next == null) {last = last.preV;}else {cur.next.preV = cur.preV;}}}cur =cur.next;}}@Overridepublic void removeAllKey(int key) {ListNode cur = head;while(cur != null) {if (cur.val == key) {if (cur == head) {head = head.next;if (head != null) {head.preV = null;}}else {cur.preV.next = cur.next;if (cur.next == null) {last = last.preV;}cur.next.preV = cur.preV;}}cur = cur.next;}}@Overridepublic int size() {ListNode cur = head;int len = 0;while(cur != null){len++;cur = cur.next;}return len;}@Overridepublic void clear() {ListNode cur = head;if(cur != null){ListNode curN = cur.next;cur.preV = null;cur.next = null;cur = curN;}head = null;last = null;}@Overridepublic void display() {ListNode cur = head;while(cur != null){System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}//在指定位置进行打印public void display2(ListNode newHead) {ListNode cur = head;while (cur != null) {System.out.print(cur.val + " ");cur = cur.next;}System.out.println();}public ListNode reverseList() {if(head == null){return head;}ListNode cur = head.next;head.next = null;while(cur != null){ListNode curN = cur.next;cur.next = head;head = cur;cur = curN;}return head;}}
public interface IList {public void addFirst(int data);public void addLast(int data);public void addIndex(int index,int data);public boolean contains (int key);public void remove(int key);public void removeAllKey(int key);public int size();public void clear();public void display();}

8.链表分割

import java.util.*;/*
public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}*/
public class Partition {public ListNode partition(ListNode pHead, int x) {ListNode as = null;ListNode ae = null;ListNode bs = null;ListNode be = null;ListNode cur = pHead;while(cur != null){if(cur.val < x){if(as == null){as = ae = cur;}else{ae.next = cur;ae = ae.next;}}else{if(bs == null){bs = be =cur;}else{be.next = cur;be = be.next;}}cur = cur.next;}if(as == null){return bs;}ae.next = bs;if(bs != null){be.next = null;}return as;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/45638.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LightRAG:高效构建和优化大型语言模型应用的 PyTorch 框架

一、前言 随着大语言模型 (LLM) 的蓬勃发展&#xff0c;检索增强生成 (RAG) 技术作为一种将 LLM 与外部知识库结合的有效途径&#xff0c;受到了越来越多的关注。 然而&#xff0c;构建 LLM 应用的真正挑战在于开发者需要根据具体需求进行高度定制化&#xff0c;而现有的 RAG …

Vscode ssh远程连接Linux服务器登录时密码password无法输入

问题 最近在用Vscode远程连接Linux服务器时&#xff0c;在终端提示输入密码password的时候用键盘输入没有反应。 以为是键盘坏了&#xff0c;然后尝试复制粘贴没有用。 后来找到了原因以及解决方法&#xff0c;感谢原帖作者&#xff08;原贴链接粘在下面&#xff09; 原因 …

flutter 列表下拉框加搜索

1.使用控件搜索加下拉框dropdown_search: ^0.4.9和获取中文拼音lpinyin: ^1.1.1 2.加入中文查询和首字查询 在当中找到相应的packages&#xff0c;再在SelectDialog.dart当中加入引入拼音搜索 import package:lpinyin/lpinyin.dart; 更改匹配方法manageItemsByFilter使其可…

有必要把共享服务器升级到VPS吗?

根据自己的需求来选择是否升级&#xff0c;虚拟专用服务器 (VPS) 是一种托管解决方案&#xff0c;它以低得多的成本提供专用服务器的大部分功能。使用 VPS&#xff0c;您的虚拟服务器将与在其上运行的其他虚拟服务器共享硬件服务器的资源。但是&#xff0c;与传统的共享托管&am…

Oracle数据库加密与安全

Wallet简介&#xff1a; Oracle Wallet(即内部加密技术TDE( Transparent DataEncryption&#xff09; TDE是 Oracle10gR2中推出的一个新功能,使用时要保证Oracle版本是在10gR2或者以上 Wallet配置&#xff1a; 1.创建一个新目录&#xff0c;并指定为Wallet目录 /home/oracle…

Python爬虫技术从去哪儿网获取旅游数据,对攻略进行可视化分析,提供全面的旅游攻略和个性化的出行建议

背景 随着信息技术的快速发展和互联网的普及&#xff0c;旅游行业也迎来了数字化和智能化的变革。去哪儿网作为中国领先的在线旅游平台之一&#xff0c;提供了丰富的旅游产品和服务&#xff0c;涵盖了机票、酒店、旅游度假等各个方面。用户通过去哪儿网可以方便地查询、预订和…

STM32HAL库+ESP8266+cJSON+微信小程序_连接华为云物联网平台

STM32HAL库ESP8266cJSON微信小程序_连接华为云物联网平台 实验使用资源&#xff1a;正点原子F407 USART1&#xff1a;PA9P、A10&#xff08;串口打印调试&#xff09; USART3&#xff1a;PB10、PB11&#xff08;WiFi模块&#xff09; DHT11&#xff1a;PG9&#xff08;采集数据…

阿里云操作系统智能助手OS Copilot的实验测评报告

什么是OS Copilot 在老师的介绍下我了解到了阿里云OS Copilot这个产品&#xff0c;它是阿里云推出的一项基于人工智能技术的操作系统&#xff0c;设计用于阿里云Linux操作系统以及其他可能的云上操作系统环境&#xff0c;为用户提供智能化的系统管理和运维支持。 阿里云提供了…

Python数据分析-Excel和 Text 文件的读写操作

1.Excel和 Text 文件的读写操作 1. Text 文件读写包 import sys print(sys.argv[0]) print(__file__) print(sys.path[0]) qopen(sys.path[0] "\out.txt","w",encodingutf-8) q.write(这个是测试一下) q.close() print(done)open 语句可以打开的创建text…

【吊打面试官系列-ZooKeeper面试题】简述 Zookeeper 文件系统?

大家好&#xff0c;我是锋哥。今天分享关于 【简述 Zookeeper 文件系统?】面试题&#xff0c;希望对大家有帮助&#xff1b; 简述 Zookeeper 文件系统? Zookeeper 提供一个多层级的节点命名空间&#xff08;节点称为 znode&#xff09;。与文件系统不同的是&#xff0c;这些节…

白平衡说明

白平衡 相机白平衡的起源原理以及作用起源作用 白平衡的原理白平衡的类型应用说明 工业相机的白平衡效果对比一键白平衡的必要性一键白平衡的实现方式 相机白平衡的起源原理以及作用 起源 白平衡&#xff08;White Balance, WB&#xff09;概念的起源与色温理论密切相关。色温…

【eNSP模拟实验】单臂路由实现VLAN间通信

实验需求 如下图所示&#xff0c;辅导员办公室需要访问处在不同vlan的学生管理服务器的文件&#xff0c;那么如何实现两台终端相互通信呢&#xff1f;我们可以使用单臂路由的方式来实现。 单臂路由&#xff08;router-on-a-stick&#xff09;是指在路由器的一个接口上通过配置…

Spring Boot中@Async注解的使用及原理 + 常见问题及解决方案

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

VMWare 下给Centos扩容

目录 参考文档背景介绍扩容查看当前文件磁盘信息增加一个存储分区创建物理卷把物理卷添加到卷组查看卷组名把物理卷并入卷组 对文件系统进行扩容搞定 参考文档 1、百度经验 2、CSDN 3、掘金 背景介绍 测试环境用VMWare 安装centos7&#xff0c;几年下来磁盘空间不够用了&…

【前端项目笔记】10 项目优化上线

项目优化上线 目标&#xff1a;优化Vue项目部署Vue项目&#xff08;上线提供使用&#xff09; 项目优化 项目优化策略&#xff1a; 生成打包报告&#xff1a;根据生成的报告发现问题并解决第三方库启用CDN&#xff1a;提高首屏页面的加载效率Element-UI组件按需加载路由懒加…

数据结构4.0——串的定义和基本操作

串的定义(逻辑结构) 串&#xff0c;即字符串(String)是由零个或多个字符组成的有序数列。 一般记为Sa1a2....an(n>0) 其中&#xff0c;S是串名&#xff0c;单引号括起来的字符序列是串的值;ai可以是字母、数字或其他字符&#xff1b;串中字符的个数n称为串的长度。n0时的…

unity 2020版本packManager没有AssetBundles

1.Packages->manifest.json打开manifest.json文件 2.添加"com.unity.assetbundlebrowser": "1.7.0", 保存即可

以数据编织,重构数据管理新范式

大数据产业创新服务媒体 ——聚焦数据 改变商业 人工智能几乎统一了全球最顶尖科技公司的认知&#xff1a;这个时代&#xff0c;除了AI&#xff0c;没有第二条路可走。 人工智能的技术逻辑颇有一种“暴力美学”&#xff0c;它依托于海量大数据和超高算力的训练和推理&#xff…

SpringBoot新手快速入门系列教程十一:基于Docker Compose部署一个最简单分布式服务项目

我的教程都是亲自测试可行才发布的&#xff0c;如果有任何问题欢迎留言或者来群里我每天都会解答。 如果您还对于Docker或者Docker Compose不甚了解&#xff0c;可以劳烦移步到我之前的教程&#xff1a; SpringBoot新手快速入门系列教程九&#xff1a;基于docker容器&#xff…

218.贪心算法:分发糖果(力扣)

核心思想 初始化每个学生的糖果数为1&#xff1a; 确保每个学生至少有一颗糖果。从左到右遍历&#xff1a; 如果当前学生的评分高于前一个学生&#xff0c;则当前学生的糖果数应比前一个学生多一颗。从右到左遍历&#xff1a; 如果当前学生的评分高于后一个学生&#xff0c;则…