总结:链表

目录

 创建链表

遍历链表

得到单链表的长度

头插法

尾插法

在任意位置插入

删除选择的一个节点

删除选择的所有节点

清空链表

找中间节点

找倒数第k个节点

反转链表

合并两个有序链表

判断是否为回文数


 

  val:存储数据

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

*判断是否包含某个数(contains):

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;//返回中间节点
    }

 *找倒数第k个节点

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;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

发呆的百香果子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值