Loading

LeetCode专题01_链表专题

链表专题

21. 合并两个有序链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null) return l2;
        else if(l2 == null) return l1;

        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while(l1!=null && l2!=null){
            if(l1.val < l2.val){
                cur.next = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }

        if(l1!=null) cur.next = l1;
        if(l2!=null) cur.next = l2;
        return dummy.next;

    }
}

82. 删除排序链表中的重复元素 II

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;

        ListNode p = dummy;
        while(p.next != null){
            ListNode q = p.next;
            while(q != null && q.val == p.next.val){
                q = q.next;
            }
            if(p.next.next == q) p = p.next;
            else p.next = q;
        }

        return dummy.next;
    }
}

83. 删除排序链表中的重复元素

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode p = head;
        while(p!=null){
            ListNode q = p;
            while(q!= null && q.val == p.val ) q = q.next;
            p.next = q;
            p = p.next;
        }

        return head;

    }
}

86. 分隔链表

思路:

开一个左链表、右链表,扫描一边,左右连接即可

public ListNode partition(ListNode head, int x) {
    
    ListNode lh = new ListNode(-1);
    ListNode rh = new ListNode(-1);
    ListNode lt = lh;
    ListNode rt = rh;
    ListNode p = head;
    while(p!=null){
        if(p.val < x){
            lt.next = p;
            lt = lt.next;
        }else{
            rt.next = p;
            rt = rt.next;
        }
        p = p.next;
    }

    lt.next = rh.next;
    rt.next = null;
    return lh.next;

}

92. 反转链表 II

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
      ListNode dummy = new ListNode(0);
      dummy.next = head;
      ListNode a = dummy;
      
      for(int i = 0; i < m - 1; i++) a = a.next;
      ListNode b = a.next;
      ListNode c = b.next;
      for(int i = 0; i < n - m; i ++){
          ListNode d = c.next;
          c.next = b;
          //调
          b = c;
          c = d;
      }

      a.next.next = c;
      a.next = b;
      return dummy.next;
    }
}

138. 复制带随机指针的链表

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

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

class Solution {

    static HashMap<Node, Node> map = new HashMap<>();
    public Node copyRandomList(Node head) {
        Node p = head;
        while(p!=null){
            map.put(p,new Node(p.val));
            p = p.next;
        }

        for(Map.Entry<Node,Node> entry: map.entrySet()){
            Node a = entry.getKey();
            Node b = entry.getValue();
            b.next = map.get(a.next);
            b.random = map.get(a.random);
        }

        return map.get(head);
        
    }
}

141. 环形链表

快慢指针算法

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null) return false;
        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;
    }
}

142. 环形链表 II

hashset记入点,第一个重复的就是入环的点

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode p = head;
        Set<ListNode> set = new HashSet<ListNode>();
        while(p!=null){
            if(set.contains(p)){
                return p;
            }else{
                set.add(p);
            }

            p = p.next;
        }
        return null;
    }
}

143. 重排链表

利用Deque可以做到

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reorderList(ListNode head) {
        ListNode p = head;
        ListNode cur = new ListNode(0);
        Deque<ListNode> dq = new LinkedList<>();

        while(p!=null){
            dq.offerLast(p);
            p = p.next;
        }

        cur = dq.pollFirst();
        while(dq.size() != 0){

            cur.next = dq.pollLast();
            cur = cur.next;
            cur.next = dq.pollFirst();
            cur = cur.next;
        }

        if(cur != null){
            cur.next = null;
        }


    }
}

148. 排序链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode merge(ListNode left, ListNode right){
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while(left!=null && right!=null){
            if(left.val <= right.val){
                cur.next = left;
                left = left.next;
            }else{
                cur.next = right;
                right = right.next;
            }

            cur = cur.next;
        }

        if(left != null) cur.next = left;
        else cur.next = right;

        return dummy.next;

    }


    public ListNode sortList(ListNode head) {

        if(head==null || head.next == null) return head;

        ListNode slow = head;
        ListNode fast = head.next;

        while(fast!=null && fast.next!= null){
            slow = slow.next;
            fast = fast.next.next;
        }

        //通过快慢指针找到链表的中点
        fast = slow.next; //第二段的起点
        slow.next = null; //第一段的终点 切开
        ListNode left = sortList(head);
        ListNode right = sortList(fast);
        return merge(left,right);
    }
}

206. 反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        ListNode a = head;
        ListNode b = a.next;
        while(b!=null){
            ListNode c = b.next;
            b.next = a;
            a = b;
            b = c;  
        }

        head.next = null;
        return a;
    }
}

234. 回文链表

先反转后面一般的指针

通过快慢指针找到中点 head - slow slow+1~...

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {

    static ListNode reverse(ListNode head){
        ListNode a = head;
        ListNode b = a.next;
        while(b!=null){
            ListNode c = b.next;
            b.next = a;
            a = b;
            b = c;
        }
        head.next = null;
        return a;
    }


    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) return true;
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast.next != null && fast.next.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }


        //此时slow就是第一段的最后一个点
        ListNode right = reverse(slow.next);
        ListNode left = head;  
        slow.next = null;  //切断

        while(left!=null && right!=null){
            if(left.val!=right.val) return false;
            left = left.next;
            right = right.next;
        }

        return true;
    }
}
posted @ 2021-04-10 15:21  想用包子换论文  阅读(50)  评论(0)    收藏  举报