Java--链表(2)

mac2026-10-04  0

链表练习

1.删除第一次出现关键字Key的节点

//查找关键字Key的前驱节点 private ListNade searchPrev(int key) { ListNade cur=this.head; while(cur.next!=null){ if(cur.next.data==key){ return cur; } cur=cur.next; } return null; } //删除第一次出现关键字为key的节点 public void remove(int key){ if(this.head == null) { System.out.println("单链表为空"); return; } //0、删除的节点是否是头结点 if(this.head.data == key) { this.head = this.head.next; return; } //1、找到key的前驱 如果返回空 ListNade prev = searchPrev(key); if(prev==null){ System.out.println("没有key这个节点"); return ; } ListNade del=prev.next; //2、删除节点 prev.next=del.next; }

2.删除所有关键字为key的节点

//删除所有值为key的节点 public void removeAllKey(int key){ ListNade pre=this.head; ListNade cur=pre.next; //循环查看每个节点的下一节点的值 while (cur!=null){ if(pre.next.data==key){ pre.next=cur.next; cur=cur.next; }else{ pre=cur; cur=cur.next; } } //头节点的是为key if(this.head.data==key){ this.head=this.head.next; } }

逆置单链表⭐⭐⭐⭐⭐

方法1. 将链表中的每一节点当成单独节点,使用头插法插入节点,实现逆置。 方法2.

public ListNade reverseList(){ ListNade pre=null; ListNade newHead=null; ListNade cur =this.head; while (cur!=null){ ListNade curNext=cur.next; if(curNext==null){ newHead=cur; } cur.next=pre; pre=cur; cur=curNext; //curNext=cur.next; } return newHead; }

3.找倒数第K个节点 ①.定义快慢指针, ②.快指针先走K-1步, ③.快慢指针一起走,直到快指针为尾节点,慢指针为倒数第K个节点

public ListNade findKth(int k){ if(this.head!=null){ ListNade cur=this.head; ListNade pre=this.head; //倒数第0个节点 if(k==0){ return null; } while (k-1>0){ if(pre.next!=null){ k--; pre=pre.next; }else{ System.out.println("没有这个节点"); } } while (pre.next!=null){ pre=pre.next; cur=cur.next; } return cur; } return null; }

4.找单链表的中间节点 ①.定义快慢指针 ②.快指针2部走,慢指针1步走 ③.直到快指针为尾节点,则慢指针为中间节点

public ListNade middlenade(){ ListNade fast=this.head; ListNade slow=this.head; while (fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next; } return slow; }

5.小于x的节点在前面,大于x的在后面

public ListNade partition(int x) { ListNade cur = this.head; ListNade beforeStart = null; ListNade beforeEnd = null; ListNade afterStart = null; ListNade afterEnd = null; while (cur != null) { //cur.data < x if(cur.data < x) { //第一次插入 if(beforeStart==null) { beforeStart=cur; beforeEnd=beforeStart; }else { beforeEnd.next=cur; beforeEnd=cur; } }else { //第一次插入 if(afterStart == null) { afterStart=cur; afterEnd=afterStart; }else { afterEnd.next=cur; afterEnd=cur; } } cur=cur.next; } //判断:小于x的那一部分节点存在 if(beforeEnd==null){ return afterStart; }else{ if(afterStart==null){ return beforeStart; }else{ beforeEnd.next=afterStart; //防止形成环,最后一个节点的next要置为null afterEnd.next=null; return beforeStart; } } }

6.删除重复的节点(重复节点连在一起时)

public ListNade deleteDuplication() { //建立虚拟的一个节点 ListNade node = new ListNade(-1); ListNade cur = this.head; ListNade tmp = node; while (cur != null) { if(cur.next != null && cur.data == cur.next.data) { //1、循环 while (cur.next != null && cur.data==cur.next.data){ cur=cur.next; } //2、退出循环 cur要多走一步 cur=cur.next; // }else { //当前节点 不等于下一个节点的时候 tmp.next = cur; cur = cur.next; tmp = tmp.next; } } node=node.next; tmp.next=null; return node; }

7.判断是不是回文

public boolean chkPalindrome() { ListNade fast = this.head; ListNade slow = this.head; //找中间节点 while (fast != null && fast.next!=null) { fast = fast.next.next; slow = slow.next; } //slow为中间节点 ListNade p = slow.next; while (p != null) { ListNade pNext = p.next; //反转 p.next=slow; slow=p; p=pNext; } //slow往前 head 往后 .data不一样 返回false //直到相遇 while (this.head!=slow){ if(this.head.data==slow.data){ //偶数个时 if(head.next==slow){ return true; } this.head=this.head.next; slow=slow.next; }else{ return false; } } return true; }
最新回复(0)