連結串列經典示例

&羅毅發表於2020-12-15

連結串列反轉

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        
        
        ArrayList<Integer> arr = new ArrayList<>();
        if(listNode == null){
            return arr;
        }

        ListNode pre = listNode;
        ListNode rear = pre.next;
        pre.next = null;
        while(rear != null){
            ListNode tmp = rear.next;
            rear.next = pre;
            pre = rear;
            rear = tmp;
        }
        
        while(pre != null){
            arr.add(pre.val);
            pre = pre.next;
        }
        return arr;
    }
}

倒數第k個節點

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        
        if(head == null || k == 0){
            return null;
        }
        ListNode first = head;
        ListNode second = head;
        for(int i = 0; i < k - 1 && second != null; ++i){
            second = second.next;
        }
        
        if(second == null){
            return null;
        }
        
        while(second.next != null){
            first = first.next;
            second = second.next;
        }
        
        return first;

    }
}

連結串列合併

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        
        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }

        ListNode l1 = list1;
        ListNode l2 = list2;
        ListNode head = null;
        ListNode last = null;
        while(l1 != null && l2 != null){
            
            if(l1.val <= l2.val){
                
                if(last == null){
                    head = l1;
                    last = l1;
                }else{
                    last.next = l1;
                    last = l1;
                }
                l1 = l1.next;
                
            }else{
                
                if(last == null){
                    head = l2;
                    last = l2;
                }else{
                    last.next = l2;
                    last = l2;
                }
                
                l2 = l2.next;
            }

        }
        if(l1 != null){
            last.next = l1;
        }
        if(l2 != null){
            last.next = l2;
        }
        
        return head;

    }
}

連結串列複製

         public static RandomListNode Clone(RandomListNode pHead) {

        if (pHead == null) {
            return null;
        }

        RandomListNode first = pHead;

        while (first != null) {
            RandomListNode tmp = first.next;
            RandomListNode newNode = new RandomListNode(first.label);
            first.next = newNode;
            newNode.next = tmp;
            first = tmp;
        }

        RandomListNode two = pHead;

        while (two != null) {

            if(two.random != null){
                two.next.random = two.random.next;
            }
            two = two.next.next;

        }

        RandomListNode cur = pHead;
        RandomListNode result = pHead.next;
        
        while(cur.next != null){
            
            RandomListNode tmp = cur.next;
            cur.next = tmp.next;
            cur = tmp;
            
        }
        return result;
    }

兩個連結串列的公共節點

    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
         
        if(pHead1 == null || pHead2 == null){
            return null;
        }
        
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        int length1 = 0;
        int length2 = 0;
        while(cur1 != null){
            length1++;
            cur1 = cur1.next;
        }
        while(cur2 != null){
            length2++;
            cur2 = cur2.next;
        }
        cur1 = pHead1;
        cur2 = pHead2;
        if(length2 >= length1){
            for(int i = 0; i < length2-length1; ++i){
                cur2 = cur2.next;
            }
        }else{
            for(int i = 0; i < length1-length2; ++i){
                cur1 = cur1.next;
            }
        }
        
        while(cur1 != null){
            
            if(cur1 == cur2){
                return cur1;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }

判斷連結串列有環

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if(pHead == null){
            return null;
        }
        
        ListNode fast = pHead;
        ListNode slow = pHead;
        
        do{
           fast = fast.next;
           if(fast == null){
               return null;
           }
            fast = fast.next;
            slow = slow.next;
            
        }while(fast != slow);
        
        fast = pHead;
        while(fast != slow){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;

    }

刪除連結串列重複節點

    public ListNode deleteDuplication(ListNode pHead)
    {
        
         if(pHead == null){
            return null;
        }
        ListNode head = new ListNode(-1);
        head.next = pHead;
        ListNode pre = head;
        ListNode next1 = pHead;
        ListNode next2 = pHead.next;

        while(next2 != null){

            if(next2.val == next1.val){

                while(next2 != null && next2.val == next1.val){
                    next2 = next2.next;
                    next1 = next1.next;

                }
                
                if(next2 == null){
                    pre.next = next2;
                    break;
                    
                }else{
                    pre.next = next2;
                    next1 = next2;
                    next2 = next2.next;
                }
                

            }else{
                pre = next1;
                next1 = next2;
                next2 = next2.next;
            }

        }

        return head.next;

    }

 

 

 

 

相關文章