链表

问题一、删除重复的链表


public class Solution {
    /**
     * @param head: head is the head of the linked list
     * @return: head of the linked list
     */
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null)
        return head;
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = head;
        ListNode pre = dummy;
        while(cur!=null){
            if(cur.next!=null&&cur.next.val == cur.val){
                int val = cur.val;
                while(cur!=null&&cur.val==val)
                    cur = cur.next;
                pre.next = cur;
            }else{
                pre = cur;
                cur = cur.next;
            }
            
        }
        
        return dummy.next;
    }
}

问题二、翻转链表2


public class Solution {
    /**
     * @param head: ListNode head is the head of the linked list 
     * @param m: An integer
     * @param n: An integer
     * @return: The head of the reversed ListNode
     */
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode pre = dummy;
        
        for(int i=1;i<m;i++){
            if(pre == null)
            return null;
            pre = pre.next;
        }
        ListNode mpreNode = pre;
        ListNode mNode = pre.next;
        
        ListNode nNode = pre,postnNode = pre.next;
        for(int i=m;i<=n;i++){
            if(postnNode==null)
                return null;
            ListNode next = postnNode.next;
            postnNode.next = nNode;
            nNode = postnNode;
            postnNode = next;
        }
        
        mpreNode.next = nNode;
        mNode.next = postnNode;
        return dummy.next;
        
    }
    
}

问题三、partition-list


小的往左 大的往右

public class Solution {
    /**
     * @param head: The first node of linked list
     * @param x: An integer
     * @return: A ListNode
     */
    public ListNode partition(ListNode head, int x) {
        ListNode lDummy = new ListNode(0);
        ListNode rDummy = new ListNode(0);
        ListNode l = lDummy,r = rDummy;
        while(head!=null){
            if(head.val<x){
                l.next = head;
                l = head;
            }else{
                r.next = head;
                r=head;
            }
            head=head.next;
        }
       l.next = rDummy.next;
       r.next=null;
       return lDummy.next;
    }
}

问题四、sort-list


public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list, using constant space complexity.
     */
    public ListNode sortList(ListNode head) {
        if(head==null||head.next==null)
        return head;
        ListNode mid = findMid(head);
        
        ListNode right = sortList(mid.next);
        mid.next = null;
        ListNode left = sortList(head);
        return merge(right,left);
        
    }
    
    public ListNode findMid(ListNode head){
        
        ListNode slow = head,fast = head.next;
        while(fast!=null&&fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    
    public ListNode merge(ListNode left,ListNode right){

        ListNode Ldummy = new ListNode(0);
        Ldummy.next = left;
        
        ListNode head = Ldummy;
        while(left!=null&&right!=null){
            if(left.val<right.val){
                head.next = left;
                left = left.next;
            }else{
                 head.next = right;
                right = right.next;
            }
            head = head.next;
        }
        if(left!=null){
            head.next = left;
        }else if(right!=null){
            head.next = right;
        }
        return Ldummy.next;
    }
}

问题五、merge-k-sorted-lists

合并K个有序链表


方法一: heap(最小堆)

  public class Solution {
    /**
     * @param lists: a list of ListNode
     * @return: The head of one sorted list.
     */
     private Comparator<ListNode> listComparator=new Comparator<ListNode>(){
       public int compare(ListNode a1,ListNode a2){
           return a1.val-a2.val;
       }
     };
    public ListNode mergeKLists(List<ListNode> lists) {  
        if(lists==null||lists.size()==0)
        return null;
        Queue<ListNode> que = new PriorityQueue<>(lists.size(),listComparator);
        for(int i=0;i<lists.size();i++){
            if(lists.get(i)!=null)
            que.add(lists.get(i));
        }
        
        ListNode dummy= new ListNode(1);
        ListNode cur = dummy;
        while(!que.isEmpty()){
            ListNode node = que.poll();
            cur.next = node;
            cur = cur.next;
            if(node.next!=null)
            que.add(node.next);
        }
        return dummy.next;
        
    }
}

方法二:

    二分 merge    

  public ListNode mergeKLists(List<ListNode> lists) {  
      if(lists==null||lists.size()==0)
      return null;
      
      return mergeHelper(lists,0,lists.size()-1);  
    }
    public ListNode mergeHelper(List<ListNode> lists,int begin,int end){
        if(begin==end)
        return lists.get(begin);
        
        ListNode head1 = mergeHelper(lists,begin,begin+(end-begin)/2);
         ListNode head2 = mergeHelper(lists,begin+(end-begin)/2+1,end);
         
        return merge(head1,head2);
    }
    
    public ListNode merge(ListNode head1,ListNode head2){
        if(head1==null)
        return head2;
        if(head2==null)
        return head1;
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while(head1!=null&&head2!=null){
            if(head1.val<head2.val)
            {
                cur.next = head1;
               
                head1 = head1.next;
            }else{
                  cur.next = head2;
                
                head2 = head2.next;
            }
            cur = cur.next;
        }
        if(head1!=null)
        cur.next = head1;
        if(head2!=null)
        cur.next = head2;
        return dummy.next;
    }
}

问题六、opy-list-with-random-pointer

问题描述:复制随机链表

public class Solution {
    /**
     * @param head: The head of linked list with a random pointer.
     * @return: A new head of a deep copy of the list.
     */
    public RandomListNode copyRandomList(RandomListNode head) {
        // write your code he
        if(head==null){
            return null;
        }
        RandomListNode cur = head;
        while(cur!=null){
            RandomListNode cpy = new RandomListNode(cur.label);
            cpy.next = cur.next;
            cur.next = cpy;
            cur = cpy.next;
        }
        
        cur = head;
        RandomListNode fast = cur.next;
        while(cur!=null){
            if(cur.random!=null)
            fast.random = cur.random.next;
            cur = fast.next;
            if(cur!=null)
            fast = cur.next;
        }
        cur = head;
        fast = cur.next;
        RandomListNode dummy = new RandomListNode(0);
        dummy.next = fast;
        fast = cur.next;
        while(cur!=null){
            cur.next = fast.next;
            cur = cur.next;
            if(cur!=null){
            fast.next = cur.next;
            fast = fast.next;
                
            }
        }
        return dummy.next;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值