链表结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e09c823d1e4ce9b83e7309a106f2a91a.png)
操作
头插
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8a726179d091c4a17a631b00f1bd9d49.png)
尾插
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0c87d1b2cfccaeca6abd6b6eebaeeb2b.png)
任意位置插入
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9637cba851f107c30b812e78066b8e0e.png)
删除一个节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/33d8d829367d9af08a4630c1376d8fab.png)
删除所有值为key的节点
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/633a93c95ed7317833198efbc64a5a1e.png)
反转链表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/133b0f142d72dab048531bb1cd46021b.png)
class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.next=null;
this.val=val;
}
}
class SignalList {
public ListNode head;
public SignalList() {
this.head = null;
}
public void addFirst(int val) {
ListNode node = new ListNode(val);
if (this.head == null) {
this.head = node;
} else {
node.next = this.head;
this.head = node;
}
}
public void addLast(int val) {
ListNode node = new ListNode(val);
ListNode cur = this.head;
if (this.head == null) {
this.head = node;
} else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
public int getLength() {
ListNode cur = this.head;
int count = 0;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
private ListNode searchIndex(int index) {
ListNode pre = this.head;
int count = 0;
while (count < index - 1) {
count++;
pre = pre.next;
}
return pre;
}
public boolean addIndex(int index, int val) {
ListNode cur = this.head;
if (index < 0 || index > getLength()) {
System.out.println("index不合法");
return false;
}
if (index == 0) {
addFirst(val);
return true;
} else {
ListNode pre = searchIndex(index);
ListNode node = new ListNode(val);
node.next = pre.next;
pre.next = node;
}
return true;
}
public boolean contains(int val) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == val) {
return true;
}
cur = cur.next;
}
return false;
}
private ListNode searchprev(int val) {
ListNode prev = this.head;
while (prev != null) {
if (prev.next.val == val) {
return prev;
}
prev = prev.next;
}
return null;
}
public void remove(int val) {
if (this.head == null) {
System.out.println("单链表为空");
return;
}
if (this.head.val == val) {
this.head = this.head.next;
}
ListNode prev = searchprev(val);
ListNode del = prev.next;
prev.next = del.next;
}
public void removeAllKey(int val) {
ListNode pre = this.head;
ListNode cur = pre.next;
while (cur != null) {
if (pre.next.val == val) {
pre.next = cur.next;
cur = cur.next;
} else {
pre = pre.next;
cur = cur.next;
}
if (this.head.val == val) {
this.head = this.head.next;
}
}
}
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public void display2(ListNode newHead) {
ListNode cur = newHead;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public ListNode reverseList() {
ListNode cur = this.head;
ListNode newHead = null;
ListNode pre = null;
while (cur != null) {
ListNode curNext = cur.next;
if (curNext == null) {
newHead = cur;
}
cur.next = pre;
pre = cur;
cur = curNext;
}
return newHead;
}
public ListNode middleNode() {
ListNode fast = this.head;
ListNode slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public ListNode findKthToTail(int k) {
ListNode fast = this.head;
ListNode slow = this.head;
if (k <= 0) {
System.out.println("k不合法");
return null;
}
while (k - 1 > 0) {
if (fast.next != null) {
fast = fast.next;
k--;
} else {
return null;
}
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
ListNode node = new ListNode(-1);
ListNode temp = node;
while (headA != null && headB != null) {
if (headA.val < headB.val) {
temp.next = headA;
headA = headA.next;
temp = temp.next;
} else {
temp.next = headB;
headB = headB.next;
temp = temp.next;
}
}
if (headB != null) {
temp.next = headB;
}
if (headA != null) {
temp.next = headA;
}
return node.next;
}
public ListNode partition(int x) {
ListNode cur=this.head;
ListNode beforeStart=null;
ListNode beforeEnd=null;
ListNode afterStart=null;
ListNode afterEnd=null;
while(cur!=null) {
if(cur.val<x) {
if(beforeStart==null) {
beforeStart=cur;
beforeEnd=beforeStart;
}else {
beforeEnd.next=cur;
beforeEnd=beforeEnd.next;
}
}else {
if (afterStart == null) {
afterStart = cur;
afterEnd = afterStart;
} else {
afterEnd.next = cur;
afterEnd = afterEnd.next;
}
}
cur=cur.next;
}
if(beforeStart==null) {
return afterStart;
}
beforeEnd.next=afterStart;
if(afterStart!=null) {
afterEnd.next = null;
}
return beforeStart;
}
public ListNode deleteDuplication() {
ListNode cur=this.head;
ListNode node=new ListNode(-1);
ListNode temp=node;
while(cur!=null) {
if(cur.next!=null&&cur.val==cur.next.val) {
while(cur.next!=null&&cur.val==cur.next.val) {
cur=cur.next;
}
cur=cur.next;
}
else{
temp.next=cur;
cur=cur.next;
temp=temp.next;
}
}
temp.next=null;
return node.next;
}
public boolean chkPalindrome() {
ListNode fast = this.head;
ListNode slow = this.head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode p = slow.next;
while (p != null) {
ListNode pNext = p.next;
p.next = slow;
slow = p;
p = pNext;
}
while (slow != head) {
if (slow.val != head.val) {
return false;
}
if (slow == head.next) {
return true;
}
slow = slow.next;
head = head.next;
}
return true;
}
public static ListNode getIntersectionNode(ListNode headA,ListNode headB){
if (headA == null||headB==null) {
return null;
}
ListNode pL=headA;
ListNode pS=headB;
int lenA=0;
int lenB=0;
ListNode cur1=headA;
ListNode cur2=headB;
while(cur1!=null) {
lenA++;
cur1=cur1.next;
}
while(cur2!=null) {
lenB++;
cur2=cur2.next;
}
int len=lenA-lenB;
if(len<0){
pL=headB;
pS=headA;
len=lenB-lenA;
}
while(len>0) {
pL= pL.next;
len--;
}
while(pL!=pS&&pL!=null) {
pL= pL.next;
pS=pS.next;
}
if(pL==pS&&pS!=null) {
return pL;
}
return null;
}
public boolean hasCycle() {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
public ListNode detectCycle() {
ListNode fast=this.head;
ListNode slow=this.head;
while(true) {
if(fast==null||fast.next==null) {
return null;
}
fast=fast.next.next;
slow=slow.next;
if(fast==slow) {
break;
}
}
fast=head;
while(fast!=slow) {
fast=fast.next;
slow=slow.next;
}
return fast;
}
public class Test {
public static void main(String[] args) {
SignalList list1=new SignalList();
SignalList list=new SignalList();
list1.addLast(421);
list1.addLast(305);
list1.addLast(13);
list1.addLast(425);
list1.addLast(222);
list1.addLast(609);
list1.addLast(100);
list1.addLast(131);
list1.addLast(579);
list1.addLast(579);
list1.addLast(131);
list1.addLast(100);
list1.addLast(609);
list1.addLast(222);
list1.addLast(425);
list1.addLast(13);
list1.addLast(305);
list1.addLast(421);
list1.display();
System.out.println(list1.chkPalindrome());
}
}