leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
来源:互联网 发布:php post 接收 编辑:程序博客网 时间:2024/05/22 19:16
题目链接
【题目】
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
【分析】
做法(1):一个指针+count len(链表)
做法(2):两个指针
做法(3):两个指针+增加一个pre指针
做法(4):二级指针
做法(5):递归
附上最优runtime
C++:
一个指针+count len(链表)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if( head == NULL ) return NULL; ListNode * pre = head; int count = 0; while( pre != NULL ){ count ++; pre = pre->next; } int m = count - n - 1; int c = 0; ListNode * p = head; if( m == -1 ) return head->next; while( c != m ){ c ++; p = p->next; } ListNode *temp = p->next->next; if(temp == NULL) p->next = NULL; else p->next = temp; return head; }};
两个指针+pre指针
class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if( head == NULL ) return NULL; ListNode * pre = NULL, * current = head, * end = head; for(int i = 0 ; i < n ; i ++ ) end = end->next; while( end ){ pre = current; current = current->next; end = end->next; } if( pre == NULL ) return head->next; else pre->next = current->next; return head; }};
链表头增加一个节点:
class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { if( head == NULL ) return NULL; ListNode * new_head = new ListNode(0); new_head->next = head; ListNode * current = new_head, * end = new_head; for(int i = 0 ; i <= n ; i ++ ) end = end->next; while( end ){ current = current->next; end = end->next; } current->next = current->next->next; return new_head->next; }};
二级指针:
参考学习二级指针
class Solution{public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode** t1 = &head, *t2 = head;//t1 == pre *t1 = current (*t1)->next = current->next for(int i = 0; i < n; ++i) { t2 = t2->next; } while(t2 != NULL) { t1 = &((*t1)->next); t2 = t2->next; } *t1 = (*t1)->next; return head; }};
递归:
class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int& n) { ListNode * node = new ListNode(0); node->next = head; removeNode(node, n); return node->next; } void removeNode(ListNode* head, int& n){ if(!head) return; removeNode(head->next, n); n--; if(n == -1) { head->next = head->next->next; } return; }};
python
构造链表:
def removeNthFromEnd(self, head, n): self.next, nodelist = head, [self] while head.next: if len(nodelist) == n: nodelist.pop(0) nodelist += head, head = head.next nodelist[0].next = nodelist[0].next.next return self.next
新建链表:
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def removeNthFromEnd(self, head, n): collection=list(); ptr=head; collection.append(ptr) while ptr.next is not None: collection.append(ptr.next) ptr=ptr.next length=len(collection); if length-n==0 and length>1: return collection[1] elif length-n==0 and length==1: return None elif n>1: collection[length-n-1].next=collection[length-n+1] return head elif n==1: collection[length-2].next=None return head
两个指针:
class Solution(object): def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ current = end = self self.next = head while end.next: if n: n -= 1 else: current = current.next end = end.next current.next = current.next.next return self.next
两个指针另一种写法:
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def removeNthFromEnd(self, head, n): current = head end = head for i in range(0,n): end = end.next if(end == None): return head.next while(end.next != None): end = end.next current = current.next current.next = current.next.next return head
这个效率比较高
0 0
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- [LeetCode-19] Remove Nth Node From End of List(删除倒数第N个节点)
- [C++]LeetCode 19: Remove Nth Node From End of List(删除链表中倒数第n个节点)
- LeetCode(Remove Nth Node From End of List)删除链表倒数第n个节点
- Remove Nth Node From End of List 删除链表倒数第N个节点
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- 【LeetCode】Remove Nth Node From End of List 删除链表中倒数第n个节点- Easy +
- leetcode:Remove Nth Node From End of List(删除链表倒数第n个节点)【面试算法题】
- Remove Nth Node From End of List(从链表表尾删除第n个节点)
- #19 Remove Nth Node From End of List(删除链表倒数第N个节点)
- 174.Remove Nth Node From End of List-删除链表中倒数第n个节点(容易题)
- LeetCode--Remove Nth Node From End of List(移除链表的倒数第N个节点)Python
- [Leetcode]_19 Remove Nth Node From End of List
- [LeetCode]—Remove Nth Node From End of List 删除链表的倒数第n个节点
- leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法
- leetcode_19. Remove Nth Node From End of List 删除单链表中的倒数第n个节点,双指针法
- leetcode-19. Remove Nth Node From End of List(删除链表倒数第N个节点)
- Remove Nth Node From End of List 从链表中删除倒数第N的节点
- java面试中经常会被问到的一些算法的问题
- 利用BP神经网络教计算机识别语音特征信号(代码部分SS)
- Java 抽象类
- 炫酷的jQuery手风琴图片和菜单插件及源码
- js缓动动画及其封装
- leetcode_[python/C++]_19. Remove Nth Node From End of List(删除链表末第n个节点)
- js浏览器区域 宽高 可视宽高 client家族等
- js事件对象event pageY screenY clientY
- 嵌入式LINUX新手问题大全——常见错误已及解决方法
- linux passwd 设置 用户 空密码
- 聚类算法理论介绍
- git clone克隆时出现的种种问题
- Ubuntu16.04 编译OpenCV 和 Tesseract-OCR
- 顺时针打印矩阵