目录
创建链表
遍历链表
得到单链表的长度
头插法
尾插法
在任意位置插入
删除选择的一个节点
删除选择的所有节点
清空链表
找中间节点
找倒数第k个节点
反转链表
合并两个有序链表
判断是否为回文数
next:存储下一个节点的地址
*链表物理上不一定连续的,逻辑上是连续的,顺序表物理上和逻辑上都是连续的
package work;
//Node类
public class Node {
private int val;
public Node next;
public Node() {
}
public Node(int val) {
this.val = val;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public String toString() {
return "Node{val = " + val + ", next = " + next + "}";
}
}
//mian方法
public static void main(String[] args) {
Node node1 = new Node(12);
Node head=node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next=node2;
node2.next=node3;
ackage work;
public class demo8 {
//遍历方法
public static void display(Node head){
Node cur=head;//用cur代替head,让head值不会一直改变
while (cur!=null){
System.out.print(cur.getVal()+" ");
cur=cur.next;
}
}
public static void main(String[] args) {
Node node1 = new Node(12);
Node head=node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next=node2;
node2.next=node3;
display(node1);//放入头节点
}
}
输出:12 13 15
public static boolean contains(int key, Node head) {
Node cur = head;
while (cur != null) {
if (head.getVal() == key) {
return true;
}
cur = cur.next;
}
return false;
}
public static int size(Node head){
int count=0;
Node cur=head;
while (cur!=null){
count++;
cur=cur.next;
}
return count;
}
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next = node2;
node2.next = node3;
int count=size(head);
System.out.println(count);
}
输出:3
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next = node2;
node2.next = node3;
//头插法
Node node=new Node(11);
node.next=head;
head=node;
display(node);
输出:11 12 13 15
//尾插法
public static void addList(Node head,int data){
Node node=new Node(data);
Node cur=head;
//要判断链表是否为空
if(head==null){
head=node;
return;
}
while (cur.next!=null){
cur=cur.next;
}
cur.next=node;
}
//调用尾插法
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next = node2;
node2.next = node3;
addList(head,16);
display(head);//从头节点开始遍历
}
private static void addIndex(Node head,int index, int data) throws ListIndexOutOfException{
//先判断索引的合法性
checkIndex(head,index);
if (index == 0) {
Node node = new Node(data);
node.next = head;
head = node;
return;
}
if (index == size(head)) {
addList(head, data);
return;
}
//循环找到索引前面的位置插入(cur)
Node cur=findIndex(index,head);
Node node=new Node(data);
node.next=cur.next;
cur.next=node;
}
private static void checkIndex(Node head,int index) {
if (index < 0 || index > size(head)) {
throw new ListIndexOutOfException("index位置不合法");
}//抛出异常
}
public static Node findIndex(int index, Node head) {
Node cur = head;
int count=0;
while (count!=index-1){//遍历直到索引前面的位置插入
cur=cur.next;
count++;//计算步数,在三索引就走两步在三前面插
}
return cur;//返回插入位置
}
//异常类
package work;
public class ListIndexOutOfException extends RuntimeException{
public ListIndexOutOfException(String message) {
super(message);
}
}
//main
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next = node2;
node2.next = node3;
try {
addIndex(head,2,14);
}catch (ListIndexOutOfException e){
e.printStackTrace();
}
display(head);
}
}
输出:12 13 14 15
public static Node searchPrey(Node head, int key) {
//没有头节点
if (head == null) {
return null;
}
Node cur = head;
while (cur.next != null) {//遍历到尾巴
if (cur.next.getVal() == key) {
return cur;//返回要删的节点的前一个节点
}
cur = cur.next;
}
return null;//没有要删除的节点
}
//main方法
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
node1.next = node2;
node2.next = node3;
Node cur=searchPrey(head,13);
if(cur==null){
return;
}
Node del=cur.next;
cur.next=del.next;
display(head);
}
public static Node removeAllkey(Node head, int key) {
if (head == null) {
return null;
}
Node prev = head;
Node cur = head.next;
while (cur != null) {
if (cur.getVal() == key) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
if(head.getVal()==key){//最后判断头节点是不是key
head=head.next;
}
return head;
}
// main方法
public static void main(String[] args) {
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
Node node4 = new Node(12);
node1.next = node2;
node2.next = node3;
node3.next = node4;
Node head1=removeAllkey(head,12);
display(head1);
}
输出:13 15
public static Node clear(Node head) {
head = null;
return head;
}
Node node1 = new Node(12);
Node head = node1;
Node node2 = new Node(13);
Node node3 = new Node(15);
Node node4 = new Node(12);
node1.next = node2;
node2.next = node3;
node3.next = node4;
Node head1 = removeAllkey(head, 12);
display(head1);
System.out.println();
head1=clear(head1);
display(head1);
}
输出:13 15
结束
public static Node middleNode(Node head){
Node fast=head;
Node slow=head;//快慢指针
while (fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;//fast走两步,slow走一步
}
return slow;//返回中间节点
}
public static Node findD(Node head,int k){
if(k<=0||head==null){
return null;
}
Node fast=head;
Node slow=head;
while (k-1!=0){//fast走k-1步
fast=fast.next;
if(fast==null){//大于size()则为null
return null;
}
k--;
}
while (fast.next!=null){//开始一步一步走
fast=fast.next;
slow=slow.next;
}
return slow;//slow即要找的k
}
public static Node fanzhuan(Node head) {
if(head==null){
return null;
}
if(head.next==null){
return head;
}
Node cur=head.next;
head.next=null;
while (cur != null) {
Node curNext = cur.next;
cur.next = head;
head = cur;
cur = curNext;
}
return head;
}
public static void main(String[] args) {
Node node1 = new Node(12);
Node node2 = new Node(45);
Node node3 = new Node(23);
Node node4 = new Node(90);
node1.next = node2;
node2.next = node3;
node3.next = node4;
Node head=fanzhuan(node1);
display(head);
输出:90 23 45 12
public Node mergeTwoLists(Node head1, Node head2) {
Node newhead = new Node();
Node temp = newhead;
while (head1 != null && head2 != null) {
if (head1.getVal() < head2.getVal()) {
temp.next = head1;
head1 = head1.next;
temp = temp.next;
} else {
temp.next = head2;
head2 = head2.next;
temp = temp.next;
}
}
if (head1 != null) {
temp.next = head1;
}
if (head2 != null) {
temp.next = head2;
}
return newhead.next;
}
public static void main(String[] args) {
LinkList linkList1=new LinkList();
linkList1.addList(1);
linkList1.addList(3);
linkList1.addList(5);
linkList1.addList(7);
linkList1.display(linkList1.head);
LinkList linkList2=new LinkList();
linkList2.addList(2);
linkList2.addList(4);
linkList2.addList(6);
linkList2.addList(8);
Node head= linkList1.mergeTwoLists(linkList1.head,linkList2.head);
System.out.println();
linkList2.display(head);
输出:1 3 5 7
1 2 3 4 5 6 7 8
public boolean PalindromeNumber() {
Node fast = head;
Node slow = head;
while (fast != null && slow != null) {
fast = fast.next.next;
slow = slow.next;
}//找中间节点
Node cur = slow.next;//要翻转的节点
while (cur != null) {
Node curNext = cur.next;
cur.next = slow;
slow = cur;
cur = curNext;
}
while (slow != head) {
if (head.getVal() != slow.getVal()) {
return false;
}
if (head.next == slow) {//只有两个数
return true;
}
slow = slow.next;
head = head.next;
}
return true;
}