zoukankan      html  css  js  c++  java
  • LeetCode-C#实现-链表(#19/21/141/206/707/876)

    19. Remove Nth Node From End of List

    删除倒数第N个结点

    解题思路

    声明两个指针,快指针先移动n次,然后快慢指针同时移动,直到快指针为null。

    此时慢指针指向了倒数第n个结点的前驱结点,然后删除慢指针后继结点即可。

    需要注意的是当删除倒数第n(n与链表长度相同)时,快指针会直接移动到null。

    此时需要单独处理,直接将head指向head的后继结点即可。

    public ListNode RemoveNthFromEnd(ListNode head, int n) {
        ListNode zero=new ListNode(0);
        zero.next=head;
        ListNode fast=zero;
        ListNode slow=zero;
        //若链表只有一个结点,直接清空
        if(head.next==null){
            head=null;
            return head;
        }
        //将快指针放向前移动n次
        for(int i=1;i<=n+1;i++)
            fast=fast.next;
        //如果快指针已经为空,说明清除的是第一个结点,直接将head指向其后继结点
        if(fast==null){
            head=head.next;
            return head;
        }
        //循环遍历链表直到快指针到达尾部
        while(fast!=null){
            fast=fast.next;
            slow=slow.next;
        }
        //此时slow的后继结点为要删除的倒数第n个结点
        slow.next=slow.next.next;
        return zero.next;
    }    

    21. Merge Two Sorted Lists

    合并两个有序链表

    解题思路

    使用递归方法调用自身,让结点一直递归到两个链表中其中一个比另一个尾部的结点大的结点。

    到最后一次调用自身,会触发两个if中的一个,返回链表剩余的结点。

    每次返回链表的首节点都会成为上一个结点的后继结点,不断返回后,就实现了两个链表有序的合并。

    public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
            if(l1==null)return l2;
            if(l2==null)return l1;
            ListNode newList=null;
            //递归调用自身,递归到两链表中最后剩的最大的链表,赋值为前一个小值的next
            if(l1.val<=l2.val){
                    newList=l1;
                    newList.next=MergeTwoLists(l1.next,l2);
            }
            else{
                    newList=l2;
                    newList.next=MergeTwoLists(l1,l2.next);
            }
            return newList;
    }

     

    141. Linked List Cycle

    检查链表中是否有环

    解题思路

    使用快慢指针,快指针一次步进慢指针的n倍,如果存在环,二者会不断循环,最终会有追上且二者相等的情况。

    如果不存在环,快指针或快指针的后继结点肯定会等于null,返回false。

    public bool HasCycle(ListNode head) {
            //快慢指针检测单链表中是否存在环
            if(head==null||head.next==null)return false;
            //快指针一次前进n倍慢指针的步数
            ListNode fastNode=head.next;
            ListNode slowNode=head;
            //如果存在环,二者一定会碰上,得到相等的情况
            while(fastNode!=slowNode){
                    if(fastNode==null||fastNode.next==null)return false;
                    fastNode=fastNode.next.next;
                    slowNode=slowNode.next;
            }
            return true;
    }

     

    206. Reverse Linked List

    反转链表

    解题思路

    反转链表的核心就是让等待被提到前面的结点标记为当前结点,让等待别的结点到自己前面的结点称为前驱结点,记录下当前结点的后继结点。

    然后将当前结点提到前驱结点前,第一次循环时,链表会断开。然后更新当前结点、前驱结点。

    之后的循环每次都是先记录下当前结点的后继结点作为下一次循环的当前结点,再执行提前和更新操作。

    public ListNode ReverseList(ListNode head) {
            if(head==null)return head;
            ListNode pre=head;
            ListNode cur=head.next;
            ListNode temp;
            while (cur!= null)
            {
                    //第一次循环时将第二个结点提到第一个节点前,此时链表断开。
                    //之后的循环就是不断将后部分的首结点提到前部分的首位。
                    //同时更新pre、cur和temp指针为前部分的首结点、后部分的首结点和后部分首结点的后继结点。 
                    temp=cur.next;//设置后继结点指针
                    cur.next=pre;//将当前结点提到前驱结点前
                    pre=cur;//设置前驱结点指针
                    cur=temp;//设置当前结点指针
            }
            head.next=null;
            return pre;
    }

     

    707. Design Linked List

    设计链表

    解题思路

    在链表类中实现这些功能:

    get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1

    addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。

    addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。

    addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。

    deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

    public class MyLinkedList {
    
        //单链表结点类
        public class Node{
            public int val;
            public Node next;
            //实例构造器
            public Node(int item){
                val=item;
                next=null;
            }
            public Node(){
                val=0;
                next=null;
            }
        }
        //头结点和数量
        public Node head;
        public int count;
        /** Initialize your data structure here. */
        public MyLinkedList() {
            head=null;
            count=0;
        }
        
        /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
        public int Get(int index) {
            //判断索引是否越界
            if(index>=count)
                return -1;
            Node p=head;
            int num=0;
            //遍历链表,找到索引指向的结点位置,返回该结点的值
            while(true){
                if(num==index)
                    return p.val;
                num++;
                p=p.next;
                
            }
        }
        
        /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
        public void AddAtHead(int val) {
            //更新head
            Node p=new Node(val);
            p.next=head;
            head=p;
            count++;
        }
        
        /** Append a node of value val to the last element of the linked list. */
        public void AddAtTail(int val) {
            Node p=new Node(val);
            Node q=head;
            //遍历链表找到尾部,添加节点
            while(q.next!=null){
                q=q.next;
            }
            q.next=p;
            count++;
        }
        
        /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
        public void AddAtIndex(int index, int val) {
            //索引为0直接执行在头结点前添加结点的方法
            if(index==0){
                AddAtHead(val);
                return;
            }
            //索引和链表长度相直接执行在尾部添加结点的方法
            if(index==count){
                AddAtTail(val);
                return;
            }
            //索引越界直接返回
            if(index>=count)
                return;
            int num=0;
            Node p=head;
            //循环遍历,在索引指向结点之前添加结点
            while(true){
                num++;
                if(num==index){
                    Node q=new Node(val);
                    q.next=p.next;
                    p.next=q;
                    count++;
                    return;
                }
                p=p.next;
            }
        }
        
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void DeleteAtIndex(int index) {
            //索引越界直接返回
            if(index>=count)
                return;
            int num=0;
            Node p=head;
            //遍历链表,删除索引指向的结点
            while(true){
                num++;
                if(num==index){
                    p.next=p.next.next;
                    count--;
                    return;
                }
                p=p.next;
            }
        }
    }
    
    /**
     * Your MyLinkedList object will be instantiated and called as such:
     * MyLinkedList obj = new MyLinkedList();
     * int param_1 = obj.Get(index);
     * obj.AddAtHead(val);
     * obj.AddAtTail(val);
     * obj.AddAtIndex(index,val);
     * obj.DeleteAtIndex(index);
     */

    876. Middle of the Linked List

    获取链表的中间结点

    解题思路

    获取其中间结点我也是使用了快慢指针,让快慢指针都以后继结点为head出发。

    快指针步进为慢指针的二倍,快指针或快指针的后继结点为null就返回慢指针,慢指针即为中间结点。

    public ListNode MiddleNode(ListNode head) {
            ListNode zero = new ListNode(0);
            zero = head;
            //快指针和慢指针以head为next开始
            ListNode fast = zero;
            ListNode slow = zero;
            if (head.next == null)
                    return head;
            while (true)
            {
                    //快慢指针步进
                    fast = fast.next.next;
                    slow = slow.next;
                    //直到快指针为null或快指针的后继结点为null,返回慢指针指向的结点
                    if (fast == null||fast.next == null)
                            return slow;
            }
    }
  • 相关阅读:
    Delphi CxGrid 用法详解
    Delphi数据库字段
    如何使DevExpress的cxGrid内容只读?
    win10系统没有Windows照片查看器怎么办 win10系统下如何找回Windows照片查看器
    Delphi XE10 dxLayoutControl 控件应用指南
    在 docker 中查看容器日志
    处理 SQL SERVER 数据库的连接查询相关问题
    nginx 禁止 ip 地址访问
    查看 SQL SERVER 的连接情况
    Linux 下同步时间,另附 NTP 服务器地址
  • 原文地址:https://www.cnblogs.com/errornull/p/9860724.html
Copyright © 2011-2022 走看看