首页 > 编程语言 >四种语言刷算法之删除链表的倒数第 N 个结点

四种语言刷算法之删除链表的倒数第 N 个结点

时间:2023-03-01 09:34:42浏览次数:59  
标签:结点 ListNode struct val int head next 链表 倒数第

力扣19. 删除链表的倒数第 N 个结点

1、C

/**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     struct ListNode *next;  * };  */ struct ListNode* removeNthFromEnd(struct ListNode* head, int n){     if(head==NULL)return NULL;     struct ListNode*p = NULL;     struct ListNode *q = head;     struct ListNode *r = head;     int count = 1;     while(r->next!=NULL){         if(count<n){             r = r->next;             count++;         }         else{             r = r->next;             p = q;             q = q->next;         }     }     if(count<n)return NULL;     if(p==NULL){         p = head->next;         free(head);         return p;     }     p->next = q->next;     free(q);     return head; }

2、C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if(head==nullptr)return nullptr;
        ListNode *p = nullptr;
        ListNode *q = head;
        ListNode *r = head;
        int count = 1;
        while(r->next!=nullptr){
            if(count<n){
                r = r->next;
                count++;
            }
            else{
                r = r->next;
                p = q;
                q = q->next;
            }
        }
        if(count<n)return nullptr;
        if(p==nullptr){
            p = q;
            q = q->next;
            delete p;
            return q;
        }
        p->next = q->next;
        delete q;
        
        return head;
    }
};

3、JAVA

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode p = null;
        ListNode q = head;
        ListNode r = head;
        int count = 1;
        while(r.next!=null){
            if(count<n){
                r = r.next;
                count++;
            }
            else{
                r = r.next;
                p = q;
                q = q.next;
            }
        }
        if(count<n)return null;
        if(p==null){
            return head.next;
        }
        p.next = q.next;
        return head;
    }
}

 

4、Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        if(head is None):
            return None
            
        p = None
        q = head
        r = head
        count = 1
        while(r.next is not None):
            if count<n:
                r = r.next
                count += 1
            else:
                r = r.next
                p = q
                q = q.next
        if count<n:
            return None
        if p is None:
            return head.next
        p.next = q.next
        return head

标签:结点,ListNode,struct,val,int,head,next,链表,倒数第
From: https://www.cnblogs.com/cmkbk/p/17153838.html

相关文章

  • 【基本数据结构】链表
    链表通过指针将一组零散的内存串联在一起,也是一种非常基础、非常常用的数据结构。 一、常见的3种链表从内存的角度来看,数组需要一块连续的内存空间,对内存的要求比较高......
  • 链表 I
    目录链表应用应用1:Leetocde.86题目解题思路代码实现链表应用应用1:Leetocde.86题目86.分隔链表解题思路在遍历过程中,将原有的链表分成两部分,使用两个\(dumy\)节点......
  • golang 实现链表爽不爽?
    犹记得刚学C语言的时候,学到指针这一章,就会有让我们写链表的需求,头插法,尾插法,翻转链表,合并链表,约瑟夫环等等学的不亦乐乎,但是对于指针刚学的时候,真是摸不着脑壳,不知道x......
  • 力扣中19 删除链表的倒数第N个节点
     就是双指针移动呢忽略了特殊情况的判断有可能是设置的标记倒数第n个节点的指针还没移动呢形如例子【1,2  n=2】或者链表很短都不存loc下下个元素会找不到溢出......
  • 链表双指针技巧
    题目难度要点分隔链表●快慢指针:不用两个新链表拼接,使用原地修改合并K个升序链表●最小堆:类ProirityQueue的使用环形链表●快慢指针:相遇有环......
  • Go组件库总结之介入式链表
    本篇文章我们用Go封装一个介入式的双向链表,目的是将链表的实现和具体元素解耦。文章参考自:https://github.com/brewlin/net-protocol1.元素的接口typeElementinterface......
  • 彻底搞懂React-hook链表构建原理
    写在前面的小结每一个hook函数都有对应的hook对象保存状态信息useContext是唯一一个不需要添加到hook链表的hook函数只有useEffect、useLayoutEffect以及us......
  • 力扣简876 链表的中间节点
    只要一个一步一步走另一个指针两步两步走然后快的走到终点慢的就是中点//只有两种情况一种中间节点有一个一种有两个分开讨论一下publicstaticListNodemiddleNo......
  • [数据结构] 单链表
    一、C语言实现1.1结构体定义typedefintElementType;//定义一个链表结构体structListNode{ElementTypeval;structListNode*next;};1.2相关方法......
  • 每日一练(剑指offer)二叉树的下一个结点
    输入描述:输入分为2段,第一段是整体的二叉树,第二段是给定二叉树节点的值,后台会将这2个参数组装为一个二叉树局部的子树传入到函数GetNext里面,用户得到的输入只有一个子树根节......