Java链表实例分析(java,开发技术)

时间:2024-05-06 00:34:56 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

1、删除值为val的所有节点

删除链表中等于给定值val的所有节点。【OJ链接】

定义两个指针prev、cur,cur指向头节点的下一个节点,prev始终指向cur的前一个结点(方便删除节点)。通过cur指针去遍历链表,和val值比较,相同就删除这个节点。最后再来比较头节点。

/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}*/classSolution{publicListNoderemoveElements(ListNodehead,intval){if(head==null){returnnull;}ListNodeprev=head;ListNodecur=head.next;while(cur!=null){if(cur.val==val){prev.next=cur.next;cur=cur.next;}else{prev=cur;cur=cur.next;}}if(head.val==val){head=head.next;}returnhead;}}

2、反转链表

反转一个链表。【OJ链接】

Java链表实例分析

在遍历链表时,将当前节点的 指针改为指向前一个节点。由于节点没有引用其前一个节点,因此必须事先存储其前一个节点。在更改引用之前,还需要存储后一个节点。最后返回新的头引用。

/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}*/classSolution{publicListNodereverseList(ListNodehead){if(head==null){returnnull;}ListNodecur=head.next;head.next=null;while(cur!=null){ListNodecurNext=cur.next;cur.next=head;head=cur;cur=curNext;}returnhead;}}

3、返回链表中间节点

给定一个带有头节点的非空单链表,返回链表的中间节点。如果有两个中间节点,则返回第二个中间节点。【OJ链接】

我们可以定义两个快慢指针(fast、slow),都指向头节点。快指针每次走两步,慢指针每次走一步。链表有偶数个节点时,fast=null时slow为中间节点;链表有奇数个节点时,fast.next=null时slow为中间节点。

Java链表实例分析

/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}*/classSolution{publicListNodemiddleNode(ListNodehead){if(head==null){returnnull;}ListNodeslow=head;ListNodefast=head;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;}returnslow;}}

4、返回链表第K个节点

输入一个链表,返回该链表中倒数第K个节点。【OJ链接】

这个题和找中间节点的思路相似。定义两个指针(fast、slow)。在K合理的前提下,我们可以让快指针先走K-1步,然后快慢指针同时向后走,当fast到达链表结尾时,slow就指向倒数第K个节点。

/*publicclassListNode{intval;ListNodenext=null;ListNode(intval){this.val=val;}}*/publicclassSolution{publicListNodeFindKthToTail(ListNodehead,intk){if(k<=0||head==null){returnnull;}ListNodefast=head;ListNodeslow=head;while(k-1>0){if(fast.next==null){returnnull;}fast=fast.next;//先让快节点走k-1步k--;}while(fast.next!=null){fast=fast.next;slow=slow.next;}returnslow;}}

5、合并有序链表

将两个有序链表合并为一个有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。【OJ链接】

Java链表实例分析

解这个题,需要定义虚假节点来充当新链表的头节点。通过两个链表的头节点去遍历两个节点,去比较两个链表对应节点的值,将值小的节点连接到新链表的后面,知道两个链表遍历完,当其中一个链表为空时,直接将另一个链表连接到新链表后面即可。

classSolution{publicListNodemergeTwoLists(ListNodelist1,ListNodelist2){if(list1==null){returnlist2;}if(list2==null){returnlist1;}//创建虚拟节点,充当新链表的头节点,值不代表任何意义ListNodenode=newListNode(-1);ListNodecur=node;while(list1!=null&&list2!=null){if(list1.val<list2.val){cur.next=list1;list1=list1.next;}else{cur.next=list2;list2=list2.next;}cur=cur.next;}if(list1==null){cur.next=list2;}else{cur.next=list1;}returnnode.next;}}

6、按值分割链表

将一个链表按照给定值X划分为两部分,所有小于X的节点排在大于或等于X的节点之前。不改变节点原来的顺序。【OJ链接】

首先我们需要定义四个指针(bs、be、as、ae)分别表示小于X部分链表的头节点和尾节点、大于X部分链表的头节点和尾节点。通过头节点遍历链表,将链表分为两部分。最后将两个链表连接起来即可。需要特别注意,当小于X部分链表不为空时,我们需要手动将ae.next置为空。

Java链表实例分析

/*publicclassListNode{intval;ListNodenext=null;ListNode(intval){this.val=val;}}*/publicclassPartition{publicListNodepartition(ListNodepHead,intx){if(pHead==null){returnnull;}ListNodebs=null;ListNodebe=null;ListNodeas=null;ListNodeae=null;ListNodecur=pHead;while(cur!=null){if(cur.val<x){if(bs==null){bs=cur;be=cur;}else{be.next=cur;be=cur;}}else{if(as==null){as=cur;ae=cur;}else{ae.next=cur;ae=cur;}}cur=cur.next;}if(bs==null){returnas;//如果小于X部分为空,则直接返回大于X部分即可。此时ae.next一定为null}be.next=as;//否则连接小于X和大于X部分if(as!=null){ae.next=null;//当小于X部分不为空时,ae.next可能不为null,需要手动置为null}returnbs;}}

7、判读回文链表

判断链表是不是回文链表。【OJ链接】

首先我们需要找到链表的中间节点,然后将后半段链表反转。最后通过两边来逐步比较即可。特别注意,当链表结点个数为偶数时,因为中间节点的缘故,两边遍历时,无法相遇,需要特殊处理。

Java链表实例分析

Java链表实例分析

/*publicclassListNode{intval;ListNodenext=null;ListNode(intval){this.val=val;}}*/publicclassPalindromeList{publicbooleanchkPalindrome(ListNodeA){if(A==null){returnfalse;}if(A.next==null){returntrue;}//求链表的中间节点ListNodeslow=A;ListNodefast=A;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;}//反转后半段链表ListNodecur=slow.next;while(cur!=null){ListNodecurNext=cur.next;cur.next=slow;slow=cur;cur=curNext;}//判断回文链表while(slow!=A){if(slow.val!=A.val){returnfalse;}if(A.next==slow){returntrue;}slow=slow.next;A=A.next;}returntrue;}}

8、找两个链表的公共节点

输入两个链表,输出两个链表的第一个公共节点。没有返回NULL。【OJ链接】

两个链表相交呈现Y字型。那么两个链表长度的差肯定是未相交前两个链表节点的差。我们需要求出两个链表的长度。定义两个指针(pl、ps),让pl指向长的链表,ps指向短的链表。求出两个链表的长度差len。让pl想走len步。这样两个链表的剩余长度就相同。此时两个指针同时遍历连个链表,如果其指向一致,则两个链表相交,否则,两个链表不相交。

/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(intx){*val=x;*next=null;*}*}*/publicclassSolution{//求链表长度publicintlen(ListNodehead){intlen=0;while(head!=null){head=head.next;len++;}returnlen;}publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){if(headA==null||headB==null){returnnull;}ListNodepl=headA;ListNodeps=headB;intlenA=len(headA);intlenB=len(headB);intlen=lenA-lenB;if(len<0){//pl指向长的链表,ps指向短的链表pl=headB;ps=headA;len=-len;}while(len--!=0){pl=pl.next;}while(pl!=null){if(pl==ps){returnpl;}pl=pl.next;ps=ps.next;}returnnull;}}

9、判断成环链表

判断链表中是否有环。【OJ链接】

还是快慢指针。慢指针一次走一步,快指针一次走两步。两个指针从链表起始位置开始运行。如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾。

/***Definitionforsingly-linkedlist.*classListNode{*intval;*ListNodenext;*ListNode(intx){*val=x;*next=null;*}*}*/publicclassSolution{publicbooleanhasCycle(ListNodehead){if(head==null||head.next==null){returnfalse;//链表为空或者只有一个节点时,没有环}ListNodeslow=head;ListNodefast=head;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;if(fast==slow){returntrue;//如果快慢节点可以相遇,表示链表有环}}returnfalse;}}

10、返回成环链表的入口

给定一个链表,判断链表是否有环并返回入环的节点。如果没有环,返回NULL。【OJ链接】

让一个指针从链表的其实在位置开始遍历,同时另一个指针从上题中两只真相与的位置开始走,两个指针再次相遇时的位置肯定为环的入口

Java链表实例分析

/***Definitionforsingly-linkedlist.*classListNode{*intval;*ListNodenext;*ListNode(intx){*val=x;*next=null;*}*}*/publicclassSolution{//判断链表是否有环,并返回第一次快慢节点相交的位置publicListNodehasCycle(ListNodehead){if(head==null||head.next==null){returnnull;}ListNodeslow=head;ListNodefast=head;while(fast!=null&&fast.next!=null){slow=slow.next;fast=fast.next.next;if(slow==fast){returnslow;}}returnnull;}//当返回的结点与头节点再次相交时,为环的入口publicListNodedetectCycle(ListNodehead){ListNodenode=hasCycle(head);if(node==null){returnnull;}else{while(head!=node){head=head.next;node=node.next;}}returnhead;}}
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java链表实例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Vue怎么实现下拉框双向联动效果下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18