算法练习(链表)

1.在这里插入图片描述

HashMap<Node , Node> map = new HashMap<>();

    public Node copyRandomList(Node head) {
    
    
       if (head == null){
    
    
           return null;
       }

       if (this.map.containsKey(head)){
    
    
           return this.map.get(head);
       }

       Node node = new Node(head.val);

       this.map.put(head,node);

       node.next = this.copyRandomList(head.next);
       node.random = this.copyRandomList(head.random);

       return node;

    }

    class Node {
    
    
        int val;
        Node next;
        Node random;

        public Node(int val) {
    
    
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }

2.在这里插入图片描述

class ListNode {
    
    
       int val;
       ListNode next;
       ListNode() {
    
    }
       ListNode(int val) {
    
     this.val = val; }
       ListNode(int val, ListNode next) {
    
     this.val = val; this.next = next; }
    }

    HashMap<ListNode,ListNode> listNodeHashMap = new HashMap<>();

    public boolean hasCycle(ListNode head) {
    
    
        /*if (head == null){//简单暴力法
            return false;
        }

        ListNode node = head;
        while (node.next != null){
            if (listNodeHashMap.containsKey(node)){
                return true;
            }
            listNodeHashMap.put(node,node);
            node = node.next;
        }
        return false;*/

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

3.在这里插入图片描述

//插入排序
public ListNode sortList(ListNode head) {
    
    
        if (head == null || head.next == null){
    
    
            return head;
        }

        ListNode node =  new ListNode(0);
        node.next = head;
        ListNode last = head;
        ListNode curr = head.next;
        while (curr != null){
    
    
            if (last.val <= curr.val){
    
    
                last = last.next;
            }else {
    
    
                ListNode pre = node;
                while (pre.next.val <= curr.val){
    
    
                    pre = pre.next;
                }
                last.next = curr.next;
                pre.next = curr;
                curr.next = pre.next;
            }
            curr = last.next;
        }
        return node.next;
    }
//归并排序(满足进阶要求)
public ListNode sortList(ListNode head) {
    
    
        return sortList(head,null);
    }

    public ListNode sortList(ListNode head , ListNode tail){
    
    
        if (head == null){
    
    
            return head;
        }

        if (head.next == tail){
    
    
            head.next = null;
            return head;
        }

        ListNode slow = head;
        ListNode fast = head;
        while (fast != tail) {
    
    
            slow = slow.next;
            fast = fast.next;
            if (fast != tail) {
    
    
                fast = fast.next;
            }
        }
        ListNode mid = slow;
        ListNode list1 = sortList(head,mid);
        ListNode list2 = sortList(mid,tail);
        ListNode start = merge(list1,list2);
        return start;
    }


    public ListNode merge(ListNode list1 , ListNode list2){
    
    
          ListNode node = new ListNode(0);
          ListNode temp = node, temp1 = list1, temp2 = list2;
          while (temp1 != null && temp2 != null){
    
    
              if (temp1.val <= temp2.val){
    
    
                  temp.next = temp1;
                  temp1 = temp1.next;
              }else {
    
    
                  temp.next = temp2;
                  temp2 = temp2.next;
              }
              temp = temp.next;
          }
          if (temp1 != null){
    
    
              temp.next = temp1;
          }else {
    
    
              temp.next = temp2;
          }
          return node.next;
    }

4.在这里插入图片描述
在这里插入图片描述

/暴力求解
 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    
        if (headA == null || headB == null){
    
    
            return null;
        }
        HashMap<ListNode,ListNode> hashMap = new HashMap<>();
        ListNode node = headA;
        while (node != null){
    
    
            hashMap.put(node,node);
            node = node.next;
        }
        node = headB;
        while (node != null){
    
    
            if (hashMap.containsKey(node)){
    
    
                return node;
            }else {
    
    
                node = node.next;
            }
        }
        return null;
    }
//两个有交点的链表,同时从两个起点出发,走完一个链表后换到另一个链表,两者一定会在交点相遇
 public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    
    
        ListNode a = headA;
        ListNode b = headB;
        while (a != b){
    
    
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }

5.在这里插入图片描述

 public ListNode reverseList(ListNode head) {
    
    
        if (head == null){
    
    
            return null;
        }
        ListNode last = null;
        ListNode curr = head;
        ListNode temp;
        while(curr != null){
    
    
            temp = curr.next;
            curr.next = last;
            last = curr;
            curr = temp;
        }
        return last;
    }

6.在这里插入图片描述

public boolean isPalindrome(ListNode head) {
    
    
         if (head == null || head.next == null){
    
    
             return true;
         }
         ListNode node1 = head;
         ListNode node2 = reverseList(HalfOfList(head));
         while (node2 != null){
    
    
             if (node1.val != node2.val){
    
    
                 return false;
             }
             node1 = node1.next;
             node2 = node2.next;
         }
         return true;
    }


    public ListNode HalfOfList(ListNode head){
    
    
        ListNode slow = head ,fast = head;
        while (fast != null && slow != null){
    
    
            slow = slow.next;
            fast = fast.next;
            if (fast != null){
    
    
                fast = fast.next;
            }
        }
        return slow;
    }

7.在这里插入图片描述
在这里插入图片描述

//只给一个节点只能这么做了
public void deleteNode(ListNode node) {
    
    
        node.val = node.next.val;
        node.next = node.next.next;
    }

8.在这里插入图片描述

 public ListNode oddEvenList(ListNode head) {
    
    
        if (head == null){
    
    
            return null;
        }
       ListNode evenHead = head.next;
       ListNode temp1 = head , temp2 = evenHead;
       while(temp2 != null && temp2.next != null){
    
    //要注意以偶节点还是奇节点为判断依据
           temp1.next = temp2.next;
           temp1 = temp1.next;
           temp2.next = temp1.next;
           temp2 = temp2.next;
       }
       temp2.next = evenHead;
       return head;
    }

猜你喜欢

转载自blog.csdn.net/cy1798/article/details/114302701