单链表的定义:
class ListNode{
public int data;
public listNode next;
public ListNode(int data){
this.data=data;
this.next=null;
}
}
class MysingalList{
public ListNode head;
public MysingalList(){
this.head=null;
}
}
头插法:
//头插法:
public void addFirst(int data){
ListNode node=new ListNode(data);
if(this.head==null){
this.head=node;
}else{
node.next=this.head;
this.head=node;
}
}
尾插法:
//尾插法:
public void addTail(int data){
ListNode pCur=this.head;
ListNode node=new ListNode(data);
if(this.head==null){
this.head=node;
}else{
while(pCur.next!=null){
pCur=pCur.next;
}
pCur.next=node;
}
}
看链表中是否包含key:
//看链表中是否包含key:
public boolean contains(int key){
ListNode pCur=this.head;
while(pCur.data!=key){
if(pCur.next==null){
return false;
}
pCur=pCur.next;
}
return true;
}
得到长度:
//得到长度:
public int getLength(){
ListNode pCur=this.head;
int count=0;
while(pCur.next!=null){
count++;
pCur=pCur.next;
}
return count;
}
删除节点
//删除节点
public void remove(int key){
ListNode del=this.head;
if(this.head==null){
return ;
}
if(this.head.data==key){
this.head=this.head.next;
return ;
}
ListNode pre=this.IndexSearch1(key);
while(del!=null){
if(del.data==key){
pre.next=del.next;
return ;
}
del=del.next;
}
}
//找前驱
private ListNode IndexSearch1(int key){
ListNode Cur=this.head;
while(Cur.next.data!=key){
Cur=Cur.next;
}
return Cur;
}
删除所有key
//删除所有key
public void removeAllKey(int key){
ListNode prev=this.head;
ListNode cur=this.head.next;
while(cur!=null){
if(cur.data==key){
prev.next=cur.next;
cur=cur.next;
}else{
prev=cur;
cur=cur.next;
}
}
if(this.head.data==key)
{
this.head=this.head.next;
}
}
倒数k个节点
//倒数k个节点
public void Function(int k){
if(k<=0||k>GetLength()){
return ;
}
ListNode pFast=this.head;
ListNode pSlow=this.head;
while(k-1>0){
pFast=pFast.next;
k--;
}
while(pFast.next!=null){
pFast=pFast.next;
pSlow=pSlow.next;
k--;
}
pSlow.next=pFast.next;
}
打印
//打印
public void disPlay(){
ListNode Cur=this.head;
while(Cur!=null){
System.out.println(Cur.data);
Cur=Cur.next;
}
}
删除重复的节点
//删除重复的节点
public ListNode deleteDuplication() {
ListNode node = new ListNode(-1);
ListNode cur = this.head;
ListNode 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;
tmp = tmp.next;
tmp.next=null;
cur = cur.next;
}
}
return node.next;
}
反转
//反转
public ListNode reverse(){
ListNode pre=null;
ListNode cur=head;
ListNode curNext=null;
while(cur!=null){
curNext=cur.next;
cur.next=pre;
pre=cur;
cur=curNext;
}
return pre;
}
回文
//回文
public boolean chkPalindrome(){
ListNode fast=head;
ListNode slow =head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
ListNode tmp=slow;
//反转
ListNode p=slow.next;
while(p!=null){
ListNode pNext=p.next;
p.next=slow;
slow=p;
p=pNext;
}
while(head!=slow){
if(this.head.data!=slow.data){
return false;
}
this.head=this.head.next;
if(this.head==slow){
return true;
}
head=head.next;
}
return true;
}
删除所有val值的节点
//删除所有val值的节点
public void removeAllKey(int val){
while(head.data==val){
this.head=this.head.next;
}
ListNode cur=this.head;
ListNode pre=this.head;
while(cur!=null){
if(cur.data==val) {
while (cur.data == val) {
cur = cur.next;
}
pre.next = cur;
}
pre=cur;
cur=cur.next;
}
}
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。
//编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 。
public ListNode fenge(int x){
ListNode beforeStart=null;
ListNode beforeEnd=null;
ListNode afterStart=null;
ListNode afterEnd=null;
ListNode cur=this.head;
while(cur!=null){
ListNode curNext=cur.next;
if(cur.data<x){
if(beforeStart==null){
beforeStart=beforeEnd=cur;
cur=curNext;
}else{
beforeEnd.next=cur;
beforeEnd=cur;
beforeEnd.next=null;
cur=curNext;
}
}else{
if(afterStart==null){
afterEnd=afterStart=cur;
cur=curNext;
}else{
afterEnd.next=cur;
afterEnd=cur;
afterEnd.next=null;
cur=curNext;
}
}
}
if(afterStart==null){
return beforeStart;
}
if(beforeStart==null){
return afterStart;
}
beforeEnd.next=afterStart;
return beforeStart;
}
判断是否是环形链表
//环形链表I
public boolean hasCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
return true;
}
}
return false;
}
找到环形链表的入口
//环形链表II
public ListNode detectCycle(ListNode head) {
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast==slow){
fast=head;
while(fast!=null&&slow!=null){
if(fast==slow){
return fast;
}
fast=fast.next;
slow=slow.next;
}
}
}
return null;
}
找到两个单链表相交的起始节点
//找到两个单链表相交的起始节点
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode aCur=headA;
ListNode bCur=headB;
ListNode pl=headA;
int countA=0;
int countB=0;
int flg=0;
while(aCur!=null){
aCur=aCur.next;
countA++;
}
while(bCur!=null){
bCur=bCur.next;
countB++;
}
int count=0;
if(countA>countB){
count=countA-countB;
}else{
count=countB-countA;
}
if(countB>countA){
pl=headB;
flg=1;
}
while(count!=0){
pl=pl.next;
count--;
}
while(pl!=null){
if(flg==1&&pl==headA){
return pl;
}
if(flg==0&&pl==headB){
return pl;
}
pl=pl.next;
headA=headA.next;
headB=headB.next;
}
return pl;
}
合并两个有序链表为一个链表
//合并两个有序链表为一个链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode node =new ListNode(-1);
ListNode tmp=node;
while(l1!=null&&l2!=null){
if(l1.data<l2.data){
tmp.next=l1;
tmp=tmp.next;
l1=l1.next;
}else{
tmp.next=l2;
tmp=tmp.next;
l2=l2.next;
}
}
if(l1==null){
tmp.next=l2;
}else{
tmp.next=l1;
}
return node.next;
}