链表是由一系列节点组成的元素集合。每个节点包含两部分,数据域 item 和指向下一个节点的指针 next。通过节点之间的相互连接,最终串联成一个链表。 ```python class Node: def __init__(self, item): self.item = item self.next = None ```  ##...
阅读全文>>2020年11月11日 20:06 作者:nancy 分类:[算法与数据结构] 1756
### 题意 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 写个函数删除一个单链表中的一个节点(非尾节点),该函数的参数是要删除的节点。 Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with va...
阅读全文>>2018年5月11日 21:39 作者:nancy 分类:[LeetCode] 1220
### 题意 Given a singly linked list, determine if it is a palindrome. 给定一个单链表,判断是否是“ 回文链表”。 Follow up: Could you do it in O(n) time and O(1) space? 你能在时间复杂度 O(n) 和空间复杂度 O(1) 下完成该问题吗? ### 思路 提取链表中的 val, 加入到 list 中,然后进行判断。 ```python ...
阅读全文>>2018年5月10日 20:32 作者:nancy 分类:[LeetCode] 1227
### 题意 Reverse a singly linked list. 反转一个单链表。 Example: ``` Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL ``` Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 你可以迭代或递归地...
阅读全文>>2018年5月9日 20:54 作者:nancy 分类:[LeetCode] 1263
### 题意 Remove all elements from a linked list of integers that have value val. 删除链表中等于给定值 val 的所有节点。 Example: ``` Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 ``` ### 思路 遍历所有节点,同时保留每个节点的上一个节点,当遇到节点值是 val 时,删除该节点。为了方便操作,定...
阅读全文>>2018年5月8日 23:02 作者:nancy 分类:[LeetCode] 1245
### 题意 Write a program to find the node at which the intersection of two singly linked lists begins. 编写一个程序,找到两个单链表相交的起始节点。 For example, the following two linked lists: ``` A: a1 → a2 ↘ c...
阅读全文>>2018年5月7日 23:34 作者:nancy 分类:[LeetCode] 1387
### 题意 Given a linked list, determine if it has a cycle in it. 给定一个链表,判断链表中是否有环。 Follow up: Can you solve it without using extra space? 你能否不使用额外空间解决此题? ### 思路 沿着链表不断遍历下去,如果遇到空节点就说明该链表不存在环。但如果存在环,这样的遍历就会进入死循环。在环上前进会不断地绕圈子,我们让两个速度不同...
阅读全文>>2018年5月6日 22:38 作者:nancy 分类:[LeetCode] 1314
### 题意 Given a sorted linked list, delete all duplicates such that each element appear only once. 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 Example 1: ``` Input: 1->1->2 Output: 1->2 ``` Example 2: ``` Input: 1->1->2->3->3 Output: 1->2->3 ``...
阅读全文>>2018年5月5日 20:37 作者:nancy 分类:[LeetCode] 1288
### 题意 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 Example: ``` Input: 1->2->4, 1->3->4 Outp...
阅读全文>>2018年5月5日 20:09 作者:nancy 分类:[LeetCode] 1211
你还没有登录,请 或者