IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> Day1:Leetcode-19.Remove Nth Node From End of List -> 正文阅读

[数据结构与算法]Day1:Leetcode-19.Remove Nth Node From End of List

Tips:There is something noteworthy in C++ code,function getlen(LinkList * head),this formal parameter?is fake,if we call this?funtion,our?actual parameter head will not change。That is different from array,for instance int fun(int a[]),it will change our actual parameter。?

Another one,if I write LinkList* head2 = head;If I change head2,it will not effect??head,or if I change head,it will not change head2 either.?

Solution 1:

? My First answer was?wrong? , I didn't??take this case into consideration.But the idea of?predecessor and?successor is not bad ,awesome,almost right.

?So I save the dommy node,do not operate the predecessor,that made a change.For this [1] case,we can not use head anymore,if head was moved,then we return head,there is no use,even we write cur -> next = null , it will not affect "head". That's the point.

Consider deletion of linklist。 We can imagin one roads,if we delete node "a","a" change the way。

class Solution {
public:
        int getlen(ListNode *head){
           int len = 0;
            while(head){
                len++;
                head = head->next;
            }
            return len;
        }

    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *head2 = head;
        int len = getlen(head);
        len = len - n + 1;
        ListNode *pre =new ListNode(0,head);
        ListNode *dommy = pre;
        ListNode *cur = head;
        for(int i = 1; i < len ;++i){
            pre = pre->next;
            cur = cur->next;
        }
        pre -> next = cur -> next;
        cur -> next = nullptr;
        return dommy->next;
    }
};
# 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):
        def getlen(head):
            length = 0;
            while head:
                length+=1
                head = head.next
            return length

        length = getlen(head)
        length = length - n + 1
        dummy =  ListNode(0,head)
        cur = dummy  
        for i in range(1,length):
            cur = cur.next
        cur.next = cur.next.next
        return dummy.next

?Solution 2

?We can use??two-pointer technique,when ed reach the end the position of st is the position to delete.?

class Solution {
public:
  
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *st  = new ListNode(0,head);
        ListNode *ed  = head;
        ListNode *dummy = st;

        for(int i=0;i<n;++i)
            ed = ed->next;

        while(ed){
            st = st->next;
            ed = ed->next;
        }
        st->next = st->next->next;
        
        return dummy->next;
    }
};
class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(0, head)
        stack = list()
        cur = dummy
        while cur:
            stack.append(cur)
            cur = cur.next
        
        for i in range(n):
            stack.pop()

        prev = stack[-1]
        prev.next = prev.next.next
        return dummy.next

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-b-61/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

?Solution 3

Stack , my favorite solution。Pay attention to python version , prev = stack[-1] means,the last element,list is circular?

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(0, head)
        first = head
        second = dummy
        for i in range(n):
            first = first.next

        while first:
            first = first.next
            second = second.next
        
        second.next = second.next.next
        return dummy.next

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-b-61/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0, head);
        stack<ListNode*> stk;
        ListNode* cur = dummy;
        while (cur) {
            stk.push(cur);
            cur = cur->next;
        }
        for (int i = 0; i < n; ++i) {
            stk.pop();
        }
        ListNode* prev = stk.top();
        prev->next = prev->next->next;
        ListNode* ans = dummy->next;
        delete dummy;
        return ans;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/solution/shan-chu-lian-biao-de-dao-shu-di-nge-jie-dian-b-61/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-09-19 08:13:59  更:2021-09-19 08:15:21 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 14:36:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码