当前位置:网站首页>Enter the starting position, the ending position intercepts the linked list
Enter the starting position, the ending position intercepts the linked list
2022-08-11 03:33:00 【这是一本书】
1 Background
Enter m n , linked list, and get a linked list with starting point m and ending point n
2 Problem-solving ideas
1) First, get N nodes according to the input n, see this article for details
https://blog.csdn.net/an13654067079/article/details/126173743
2) m node, n node m node as the head of the new linked list
3) The previous node of the m node mPre.next = null
The next node of n nodes nNext.next = null
The new linked list is intercepted, the problem is not difficult
public static void main(String[] args) {ListNode listNode = ListNodeUtil.getListNode();int m = 2;int n = 4;//Get m nodes, n nodesListNode mNode = index(listNode, m);ListNode nNode = index(listNode, n);if(m!=1){//Get the previous node of mint mPre = m-1;ListNode mPreNode = index(listNode, mPre);mPreNode.next = null;}ListNode nNextNode = nNode.next;nNode.next = null;System.out.println(mNode);}/*** Get the node at the specified location** @param listNode* @param index* @return*/public static ListNode index(ListNode listNode , int index){ListNode dummyNode = new ListNode();dummyNode.setVal(0);dummyNode.setNext(listNode);int n = 1;for(int i = 0 ; i< index; i++){dummyNode = dummyNode.getNext();}return dummyNode;}
import lombok.Data;@Datapublic class ListNode {/*** current node value*/public int val;/*** next node*/public ListNode next ;}
public class ListNodeUtil {/*** Get the linked list** @return*/public static ListNode getListNode(){ListNode one = new ListNode();one.setVal(1);ListNode two = new ListNode();two.setVal(2);ListNode three = new ListNode();three.setVal(3);ListNode four = new ListNode();four.setVal(4);ListNode five = new ListNode();five.setVal(5);one.setNext(two);two.setNext(three);three.setNext(four);four.setNext(five);return one;}}
边栏推荐
- 【FPGA】设计思路——I2C协议
- Typescript学习笔记 | 字节青训营笔记
- Qnet Weak Network Test Tool Operation Guide
- uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
- 多商户商城系统功能拆解26讲-平台端分销设置
- 用户如何克服程序化交易中的情绪问题?
- DNS separation resolution and intelligent resolution
- 【C语言】入门
- EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
- CTO说MySQL单表行数不要超过2000w,为啥?
猜你喜欢
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
分布式和集群的区别和联系
基于改进YOLOv5轻量化的烟火检测
Official release丨VS Code 1.70
电力机柜数据监测RTU
高校就业管理系统设计与实现
A simple JVM tuning, learn to write it on your resume
The development of the massage chair control panel makes the massage chair simple and intelligent
Qnet弱网测试工具操作指南
Summary of debugging skills
随机推荐
Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
Design and Realization of Employment Management System in Colleges and Universities
阿里低代码框架 lowcode-engine 之自定义物料篇
LeetCode热题(12.买卖股票的最佳时机)
Description of ESB product development steps under cloud platform
互换性与测量技术——表面粗糙度选取和标注方法
Goodbye Guangzhou paper invoices!The issuance of electronic invoices for accommodation fees will completely replace the invoices of hotels, restaurants and gas stations
E-commerce project - mall time-limited seckill function system
Leetcode 108. 将有序数组转换为二叉搜索树
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
Audio codec, using FAAC to implement AAC encoding
Kubernetes集群搭建Zabbix监控平台
云平台下ESB产品开发步骤说明
STC8H development (15): GPIO drive Ci24R1 wireless module
多串口RS485工业网关BL110
【FPGA】day20-I2C读写EEPROM
Leetcode 450. 删除二叉搜索树中的节点
A brief analysis of whether programmatic futures trading or manual order is better?
DNS分离解析和智能解析
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations