鏈表

合并排序鏈表

鏈表排序

鏈表中環(huán)的入口結點

給一個鏈表,若其中包含環(huán),請找出該鏈表的環(huán)的入口結點,否則,輸出null。

public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead){
        if(pHead==null) return null;
        ListNode p1 = pHead.next;
        if(p1==null) return null;
        ListNode p2 = p1.next;
        while(p1!=p2){
            if(p1==null||p2==null) return null;
            p1=p1.next;
            p2=p2.next;
            if(p2==null) return null;
            p2=p2.next;
        }
        p2=pHead;
        while(p1!=p2){
            p1=p1.next;
            p2=p2.next;
        }
        return p1;  
    }
}

刪除鏈表中重復的結點

在一個排序的鏈表中,存在重復的結點,請刪除該鏈表中重復的結點,重復的結點不保留,返回鏈表頭指針。 例如,鏈表1->2->3->3->4->4->5 處理后為 1->2->5

public class Main {
    public ListNode deleteDuplication(ListNode pHead)
   {
        ListNode p = new ListNode(123);
        p.next=pHead;
        ListNode head = pHead;
        ListNode last = p;
        while(head!=null){
            if(head.next==null){
                break;
            }else if(head.val!=head.next.val){
                last = head;
                head = head.next;
            }else{
                while(head.next!=null&&head.val==head.next.val){
                    head = head.next;
                }
                last.next=head.next;
                head = head.next ;
            }
        }
        return p.next;
   }
}

復制復雜鏈表

輸入一個復雜鏈表(每個節(jié)點中有節(jié)點值,以及兩個指針,一個指向下一個節(jié)點,另一個特殊指針指向任意一個節(jié)點),返回結果為復制后復雜鏈表的head。(注意,輸出結果中請不要返回參數(shù)中的節(jié)點引用,否則判題程序會直接返回空)

public RandomListNode Clone(RandomListNode phead){
        if(phead==null)
            return null;
        RandomListNode head = phead;
        while(head!=null){
            RandomListNode node = new RandomListNode(head.label);
            node.next=head.next;
            head.next=node;
            head = node.next;
        }
        head = phead;
        while(head!=null){
            if(head.random!=null){
                head.next.random=head.random.next;
            }
            head = head.next.next;
        }
        head = phead;
        RandomListNode newHead=head.next;
        RandomListNode p = newHead;
        while(p.next!=null){
            head.next = head.next.next;
            head = head.next;
            p.next = p.next.next;
            p = p.next;  
        }
        head.next=null;
        p.next=null;
        return newHead;
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容