leetcode: 19. Remove Nth Node From End of List
来源:互联网 发布:高校财经数据库 编辑:程序博客网 时间:2024/05/20 12:46
Problem
# 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.
AC
class ListNode(): def __init__(self, x): self.val = x self.next = Noneclass Solution(): def removeNthFromEnd(self, head, x): slow = fast = dummy = ListNode(0) dummy.next = head while x and fast.next: fast, x = fast.next, x - 1 while fast.next: slow, fast = slow.next, fast.next slow.next = slow.next.next return dummy.nextif __name__ == "__main__": head, head.next, head.next.next, head.next.next.next, head.next.next.next.next \ = ListNode(1), ListNode(2), ListNode(3), ListNode(4), ListNode(5) result = Solution().removeNthFromEnd(head, 2) assert '{0}->{1}->{2}->{3}'.format(result.val, result.next.val, result.next.next.val, result.next.next.next.val) \ == '1->2->3->5'
阅读全文
1 0
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- LeetCode --- 19. Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- [Leetcode] 19. Remove Nth Node From End of List
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- Leetcode:19. Remove Nth Node From End of List(JAVA)
- leetcode 19. Remove Nth Node From End of List
- Leetcode 19. Remove Nth Node From End of List
- 【LeetCode】19. Remove Nth Node From End of List
- leetcode 19.Remove Nth Node From End of List
- NUMA架构下的CPU拓扑
- wxWidgets教程(11)——App与进程管理
- angular中使用锚点
- Java 字符串Split方法的一个坑
- 深入理解机器学习:从理论到算法-习题全解之第二十章:神经元网络
- leetcode: 19. Remove Nth Node From End of List
- Javaweb-xml编程-考生成绩管理系统(2)
- STM32 gcc编译环境搭建
- vsftpd服务
- RSA不同于模数攻击的破解法
- Python 练习实例1
- 2017年11月07日WEB随笔
- Linux centos7下MySQL的安装
- 网络编程—socket编程