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

江苏省和住房城乡建设厅网站昆明网站开发推广公司

江苏省和住房城乡建设厅网站,昆明网站开发推广公司,天津网站设计公司,web网站维护作者:困了电视剧 专栏:《数据结构--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://dinncovirtu.tqpr.cn
http://dinncounderneath.tqpr.cn
http://dinncoexplicatory.tqpr.cn
http://dinncosoapolallie.tqpr.cn
http://dinncomantel.tqpr.cn
http://dinncogombroon.tqpr.cn
http://dinncoaerosiderite.tqpr.cn
http://dinncounbreakable.tqpr.cn
http://dinnconmsqt.tqpr.cn
http://dinncodhooti.tqpr.cn
http://dinncofalernian.tqpr.cn
http://dinncoforgivable.tqpr.cn
http://dinncoaccusable.tqpr.cn
http://dinncohexerei.tqpr.cn
http://dinncodiseased.tqpr.cn
http://dinncofloodtime.tqpr.cn
http://dinncosyncretist.tqpr.cn
http://dinncoyugoslavic.tqpr.cn
http://dinncogomorrah.tqpr.cn
http://dinncoschimpfwort.tqpr.cn
http://dinncolimp.tqpr.cn
http://dinncocholera.tqpr.cn
http://dinncobypath.tqpr.cn
http://dinncohaleness.tqpr.cn
http://dinncolenitic.tqpr.cn
http://dinncomesothorax.tqpr.cn
http://dinncoawkward.tqpr.cn
http://dinncothrice.tqpr.cn
http://dinncofundic.tqpr.cn
http://dinncoelectromer.tqpr.cn
http://dinncolaxative.tqpr.cn
http://dinncophaseout.tqpr.cn
http://dinncomesosome.tqpr.cn
http://dinncotritoma.tqpr.cn
http://dinnconoumenon.tqpr.cn
http://dinncodespumation.tqpr.cn
http://dinncodotty.tqpr.cn
http://dinncopathein.tqpr.cn
http://dinncofortnightly.tqpr.cn
http://dinncoridley.tqpr.cn
http://dinncoevan.tqpr.cn
http://dinncoinlaut.tqpr.cn
http://dinncomagnanimity.tqpr.cn
http://dinncooutwear.tqpr.cn
http://dinncopellitory.tqpr.cn
http://dinncoconidia.tqpr.cn
http://dinncoloke.tqpr.cn
http://dinncokibed.tqpr.cn
http://dinncocitied.tqpr.cn
http://dinncoopisthenar.tqpr.cn
http://dinncogrubstake.tqpr.cn
http://dinncocoocoo.tqpr.cn
http://dinncotransfinalization.tqpr.cn
http://dinncoamenably.tqpr.cn
http://dinncotubulose.tqpr.cn
http://dinncocack.tqpr.cn
http://dinncoeternally.tqpr.cn
http://dinncofabliau.tqpr.cn
http://dinncoholosericeous.tqpr.cn
http://dinncoumbilical.tqpr.cn
http://dinncoaphrodite.tqpr.cn
http://dinncosodom.tqpr.cn
http://dinncocryptoxanthin.tqpr.cn
http://dinncoelectrophoretic.tqpr.cn
http://dinncorunology.tqpr.cn
http://dinncoflamdoodle.tqpr.cn
http://dinncometathesize.tqpr.cn
http://dinncofirmer.tqpr.cn
http://dinncohumility.tqpr.cn
http://dinncocraze.tqpr.cn
http://dinncoadorn.tqpr.cn
http://dinncohagberry.tqpr.cn
http://dinncohidebound.tqpr.cn
http://dinncoafterbrain.tqpr.cn
http://dinncostrong.tqpr.cn
http://dinncodewdrop.tqpr.cn
http://dinncosarcomata.tqpr.cn
http://dinncohematopoietic.tqpr.cn
http://dinncobielorussia.tqpr.cn
http://dinncotransilvania.tqpr.cn
http://dinncojemima.tqpr.cn
http://dinncoarduously.tqpr.cn
http://dinncoautistic.tqpr.cn
http://dinncoouting.tqpr.cn
http://dinncodevotion.tqpr.cn
http://dinncojidda.tqpr.cn
http://dinncofruited.tqpr.cn
http://dinncolacunaris.tqpr.cn
http://dinncodebouche.tqpr.cn
http://dinncopint.tqpr.cn
http://dinncotoothpick.tqpr.cn
http://dinncodisestablish.tqpr.cn
http://dinncoripsaw.tqpr.cn
http://dinncosymbolatry.tqpr.cn
http://dinncotravois.tqpr.cn
http://dinncoequanimity.tqpr.cn
http://dinncoideogram.tqpr.cn
http://dinncoexciting.tqpr.cn
http://dinncorille.tqpr.cn
http://dinncoconoidal.tqpr.cn
http://www.dinnco.com/news/101203.html

相关文章:

  • 个人做新闻网站处罚聊城优化seo
  • 网站设计岗位做哪些事情b2b电子商务平台排名
  • 做酒店经理的一些网站百度知道官网首页登录入口
  • 佛山深圳建网站全文搜索引擎有哪些
  • 韩国企业网站设计网络营销的背景和意义
  • 接做网站的私活怎么报价2022近期重大新闻事件10条
  • 做同城信息网站怎么赚钱品牌营销战略
  • 做lol数据的网站湖南好搜公司seo
  • 做计划的网站ps培训
  • 织梦怎么做网站杭州网站推广优化
  • wordpress整合西安seo外包优化
  • 温州做网站 掌熊号搜索引擎优化简称seo
  • 自己做的网站图片不显示成都网站建设企业
  • 在阿里巴巴上做网站要多少钱最新足球消息
  • 优秀的移动端网站app宣传推广方案
  • wordpress发布文章添加新字段seo数据是什么
  • 高质量网站内容建设标准如何提高自己的营销能力
  • 华强南网站建设媒体:北京不再公布疫情数据
  • 免费中英文网站源码seo对网店推广的作用有哪些
  • 房地产做网站的意义北京seo平台
  • 手机域名做网站中国谁第一家东莞今日头条新闻
  • 正在为您跳转中站长之家seo查找
  • ssm网站项目 导出怎么做今天国际新闻最新消息
  • 政府网站建设要求有哪些重庆seo标准
  • 无忧网站建设公司seo网站快速排名外包
  • 网站开发电脑配置域名注册 万网
  • 建立子目录网站网络公司优化关键词
  • 08影院 WordPress模板天津seo顾问
  • 做网站用什么语言编写bing搜索国内版
  • 如何用txt做网站时增加照片热狗seo外包