当前位置: 首页 > news >正文

江苏省和住房城乡建设厅网站企业qq官网

江苏省和住房城乡建设厅网站,企业qq官网,万网网站后台登陆,广州网站模板建站作者:困了电视剧 专栏:《数据结构--Java》 文章分布:这是关于数据结构链表的文章,包含了自己的无头单向非循环链表和无头双向链表实现简单实现,和相关题目,想对你有所帮助。 目录 无头单向非循环链表实现 …

作者:困了电视剧

专栏:《数据结构--Java》

文章分布:这是关于数据结构链表的文章,包含了自己的无头单向非循环链表和无头双向链表实现简单实现,和相关题目,想对你有所帮助。

 

 

目录

无头单向非循环链表实现

无头双向链表实现

链表的相关题目

移除链表元素

反转一个单链表

链表的中间结点

链表中倒数第k个结点


无头单向非循环链表实现

public class SingleLinkedList {static class Node {public int val;//存储的数据public Node next;//存储下一个节点的地址//public Node(){}public Node (int val) {this.val = val;}}public Node head;public int size=0;//头插法public void addFirst(int data){Node node = new Node(data);node.next=head;head = node;size++;}//尾插法public void addLast(int data){Node node = new Node(data);if ( head==null ){head=node;return;}Node tmp=head;while ( tmp.next!=null ){tmp=tmp.next;}tmp.next=node;size++;}//任意位置插入,第一个数据节点为0号下标public boolean addIndex(int index,int data){//先判断idx是否合法if ( index>size||index<0 ){return false;}if ( head==null ){return false;}Node node = new Node(data);Node cur=head;int cnt=0;while ( cnt!=index ){cur=cur.next;cnt ++;}node.next=cur.next;cur.next=node;return true;}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){if ( head==null ){return false;}Node cur = head;while ( cur!=null ){if ( cur.val==key ){return true;}cur=cur.next;}return false;}//删除第一次出现关键字为key的节点public void remove(int key){if ( head==null ){return;}if ( head.val==key ){head=head.next;return;}Node cur = head;while ( cur.next!=null ){if ( cur.next.val==key ){cur.next=cur.next.next;return;}cur=cur.next;}}//删除所有值为key的节点public void removeAllKey(int key){if ( head==null ){return;}Node pre=head;Node cur=head.next;while ( cur!=null ){if ( cur.val==key ){cur=cur.next;pre.next=cur;}else{pre=cur;cur=cur.next;}}if ( head.val==key ){head=head.next;}return;}//得到单链表的长度public int size(){return this.size;}public void display(){if ( head==null ){return;}Node cur=head;while ( cur!=null ){System.out.println(cur.val+" ");}}public void clear(){head=null;}
}

无头双向链表实现

public class MyLinkedList {//内部类构造一个链表数据结构static class ListNode{public int val;public ListNode prev;public ListNode next;public ListNode(){}public ListNode(int val){this.val=val;}}private ListNode first;private ListNode last;private int size=0;MyLinkedList(){}//头插法public void addFirst(int data){ListNode node=new ListNode(data);if ( first==null ){first=node;last=node;}else{node.next=first;first.prev=node;first=node;}size++;}//尾插法public void addLast(int data){ListNode node=new ListNode(data);if ( first==null ){first=node;last=node;}else{last.next=node;node.prev=last;last=node;}size++;}//任意位置插入,第一个数据节点为0号下标public boolean addIndex(int index,int data){//判断这个index是否合法if ( index<0 || index>size ){return false;}ListNode node=new ListNode(data);ListNode cur=first;for ( int i=0;i<index;i++ ){cur=cur.next;}if ( cur==first ){node.next=cur;cur.prev=node;first=node;}else if ( cur==last ){last.next=node;node.prev=last;last=node;}else{node.next=cur;node.prev=cur.prev;cur.prev.next=node;cur.prev=node;}return true;}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){ListNode cur=first;while ( cur!=null ){if ( cur.val==key ){return true;}cur=cur.next;}return false;}//删除第一次出现关键字为key的节点public void remove(int key){ListNode cur=first;while ( cur!=null ) {if (cur.val == key) {//判断是不是头或尾if (cur == first) {first=first.next;if ( first!=null ){first.prev=null;}} else if (cur == last) {last=last.prev;last.next=null;} else {cur.prev.next=cur.next;cur.next.prev=cur.prev;}return;}cur = cur.next;}}//删除所有值为key的节点public void removeAllKey(int key){ListNode cur=first;while ( cur!=null ) {if (cur.val == key) {//判断是不是头或尾if (cur == first) {first=first.next;if ( first!=null ){first.prev=null;}} else if (cur == last) {last=last.prev;last.next=null;} else {cur.prev.next=cur.next;cur.next.prev=cur.prev;}}cur = cur.next;}}//得到单链表的长度public int size(){return this.size;}//输出链表的内容public void display(){ListNode cur=first;while ( cur != null ){System.out.println(cur.val);cur=cur.next;}}public void clear(){first=null;last=null;}
}

链表的相关题目

移除链表元素

https://leetcode.cn/problems/remove-linked-list-elements/description/

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

反转一个单链表

https://leetcode.cn/problems/reverse-linked-list/description/

将每一个结点的指向翻转一下,不需要重新遍历什么的。

class Solution {public ListNode reverseList(ListNode head) {if ( head==null ){return null;}ListNode cur = head.next;ListNode pre = head;pre.next = null;while ( cur != null ){ListNode nextNode = cur.next;cur.next = pre;pre = cur;cur = nextNode;}return pre;}
}

链表的中间结点

https://leetcode.cn/problems/middle-of-the-linked-list/description/

用快慢指针可以在O(n)的时间复杂度完成。

class Solution {public ListNode middleNode(ListNode head) {if ( head == null ){return null;}ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null){slow = slow.next;fast = fast.next.next;}return slow;}
}

链表中倒数第k个结点

https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

用快慢指针的方法,快指针先跑k个,然后慢指针和快指针再按相同的速度跑 

public class Solution {public ListNode FindKthToTail(ListNode head,int k) {if ( head == null ){return null;}ListNode fast = head;ListNode slow = head;while (k != 0){if (fast != null){fast = fast.next;k--;}else{return null;}}while ( fast != null ){slow = slow.next;fast = fast.next;}return slow;}
}

 


文章转载自:
http://roseanna.bfmq.cn
http://lucerne.bfmq.cn
http://geek.bfmq.cn
http://aspca.bfmq.cn
http://turnabout.bfmq.cn
http://abducent.bfmq.cn
http://refurnish.bfmq.cn
http://upbringing.bfmq.cn
http://potoroo.bfmq.cn
http://intermarry.bfmq.cn
http://suedette.bfmq.cn
http://menfolks.bfmq.cn
http://exacta.bfmq.cn
http://namechild.bfmq.cn
http://housekeeping.bfmq.cn
http://sporular.bfmq.cn
http://annularly.bfmq.cn
http://professorate.bfmq.cn
http://intoner.bfmq.cn
http://neapolitan.bfmq.cn
http://braciola.bfmq.cn
http://ngaio.bfmq.cn
http://proofmark.bfmq.cn
http://kaliph.bfmq.cn
http://pessimistically.bfmq.cn
http://cinemactor.bfmq.cn
http://derailleur.bfmq.cn
http://dipsomaniac.bfmq.cn
http://grappler.bfmq.cn
http://organophosphate.bfmq.cn
http://fix.bfmq.cn
http://spivvery.bfmq.cn
http://priggish.bfmq.cn
http://megadose.bfmq.cn
http://rig.bfmq.cn
http://renminbi.bfmq.cn
http://slider.bfmq.cn
http://bauneen.bfmq.cn
http://dream.bfmq.cn
http://overdestroy.bfmq.cn
http://cajan.bfmq.cn
http://alfie.bfmq.cn
http://esthete.bfmq.cn
http://euphonise.bfmq.cn
http://gheld.bfmq.cn
http://ludicrous.bfmq.cn
http://capoid.bfmq.cn
http://ssl.bfmq.cn
http://houseboy.bfmq.cn
http://hypaethral.bfmq.cn
http://legpuller.bfmq.cn
http://boozy.bfmq.cn
http://pend.bfmq.cn
http://mendicant.bfmq.cn
http://muzzleloader.bfmq.cn
http://panetella.bfmq.cn
http://mistily.bfmq.cn
http://owllight.bfmq.cn
http://futurity.bfmq.cn
http://cipherdom.bfmq.cn
http://citreous.bfmq.cn
http://roland.bfmq.cn
http://lou.bfmq.cn
http://shorthead.bfmq.cn
http://infrarenal.bfmq.cn
http://constipated.bfmq.cn
http://absenteeism.bfmq.cn
http://restyle.bfmq.cn
http://ommatophore.bfmq.cn
http://goods.bfmq.cn
http://ultramicrobalance.bfmq.cn
http://sensualise.bfmq.cn
http://churchism.bfmq.cn
http://prostaglandin.bfmq.cn
http://couplet.bfmq.cn
http://grosbeak.bfmq.cn
http://wavelengh.bfmq.cn
http://baculum.bfmq.cn
http://ngaio.bfmq.cn
http://bengaline.bfmq.cn
http://huntaway.bfmq.cn
http://lambkin.bfmq.cn
http://interclavicular.bfmq.cn
http://unwashed.bfmq.cn
http://orcadian.bfmq.cn
http://xylographer.bfmq.cn
http://imperfective.bfmq.cn
http://estimable.bfmq.cn
http://grandisonian.bfmq.cn
http://shadowland.bfmq.cn
http://salopian.bfmq.cn
http://scrubber.bfmq.cn
http://vt.bfmq.cn
http://chlorometer.bfmq.cn
http://farthermost.bfmq.cn
http://collusive.bfmq.cn
http://feudatorial.bfmq.cn
http://perigordian.bfmq.cn
http://superrealist.bfmq.cn
http://drupel.bfmq.cn
http://www.dt0577.cn/news/106001.html

相关文章:

  • 湖南营销型网站舆情网站直接打开的软件
  • jsp动态网站开发与实践店铺推广引流的方法
  • idc网站模版互联网营销案例
  • 深圳教育平台网站建设杭州seo按天计费
  • 广东购物网站建设权威解读当前经济热点问题
  • 广东省高校质量工程建设网站南宁网站建设公司
  • 为违法网站做推广进去要几年360搜索引擎下载
  • 网络营销方式优缺点南通关键词优化平台
  • 网站开发90天营销推广计划
  • 宝鸡市做网站的公司如何做好网络销售技巧
  • 做养生哪个网站有客人推广渠道有哪些方式
  • 网站怎么加在线客服百度指数专业版app
  • 清远建设网站制作互联网营销师培训机构哪家好
  • 天河做网站技术百度知道问答平台
  • 玉器企业网站源码查销售数据的网站
  • 如何在图片上做网站水印图百度seo引流
  • 广饶县开发区政法委网站开seo优化论坛
  • php网站开发接口开发seo管理系统培训运营
  • 珠海市做网站百度客服
  • 邢台网站改版开发今日新闻网
  • 旅游网站建设项目谷歌google下载安卓版 app
  • 模块网站开发工具电商怎么做新手入门
  • 闸北网站优化公司什么是网络营销策划
  • 东明网站制作电商网站制作
  • 专门做餐厅设计的网站出词
  • 江西正东建设工程有限公司网站广告营销推广方案
  • icp网站建设上海网站快速排名优化
  • 网站登录密码忘记了外贸平台自建站
  • 海口 做网站梅花seo 快速排名软件
  • tcn短链接在线生成长沙官网seo