• 19. 删除链表的倒数第N个节点


    19. 删除链表的倒数第N个节点

    1、题目介绍

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

    试题链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/

    2、java

    2.1、遍历两次

        public static ListNode removeNthFromEnd(ListNode head, int n) {
            if(head == null || head.next == null) return null;
            //求链表长度
            ListNode p = head;
            int size = 0;
            while (p != null)  {
                size++;
                p = p.next;
            }
    
            if(n == size) return head.next;
    
            p = head;
            for(int i = 2;i <= size - n;i++ ) {
                p = p.next;
            }
    
            p.next = p.next.next;
    
            return head;
        }
    

    测试结果:

    2.1、遍历一次

        public static ListNode removeNthFromEnd(ListNode head, int n) {
            if(head == null || head.next == null) return null;
            //求链表长度
            ListNode p = head;
            int flag = 0;
            while (p != null && p.next != null) {
                ListNode temp = p.next;
                for(int i = 0;i < n;i++) {
                    if(temp == null) {
                        flag = 1;
                        break;
                    }
                    temp = temp.next;
                }
                if(flag == 1) {
                    return head.next;
                }
                if(temp != null) {
                    p = p.next;
                }else {
                    p.next = p.next.next;
                    return head;
                }
            }
            return head;
        }
    

    测试结果:

    3、C语言

    3.1、遍历两次

    struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
        if(head == NULL || head->next == NULL) return NULL;
        //求链表长度
        struct ListNode* p = head;
        int size = 0;
        while (p != NULL)  {
            size++;
            p = p->next;
        }
    
        if(n == size) return head->next;
    
        p = head;
        for(int i = 2;i <= size - n;i++ ) {
            p = p->next;
        }
    
        p->next = p->next->next;
    
        return head;
    }
    

    测试结果:

    3.1、遍历一次

    struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
        if(head == NULL || head->next == NULL) return NULL;
        //求链表长度
        struct ListNode* p = head;
        int flag = 0;
        while (p != NULL && p->next != NULL) {
            struct ListNode* temp = p->next;
            for(int i = 0;i < n;i++) {
                if(temp == NULL) {
                    flag = 1;
                    break;
                }
                temp = temp->next;
            }
            if(flag == 1) {
                return head->next;
            }
            if(temp != NULL) {
                p = p->next;
            }else {
                p->next = p->next->next;
                return head;
            }
        }
        return head;
    }
    

    测试结果:

  • 相关阅读:
    创建异步Web服务
    MCAD考试计划
    微软面试题
    Reborn
    Qt项目注意事项
    在ASP.Net中两种利用CSS实现多界面
    为ASP.NET控件添加常用的JavaScript操作
    用Split函数分隔字符串
    Microsoft .NET Pet Shop 4.0
    搞定QString,string,char*,CString的互转
  • 原文地址:https://www.cnblogs.com/xgp123/p/12398126.html
Copyright © 2020-2023  润新知