整理之后想起来LeetCode自己家出过一个类似的汇总Orz,大概是最近真的太闲了,想整理下刷过的题目,老实讲之前刷的题并不多。 LeetCode领扣的公众号做得很好,有时间回去翻翻看。
单链表反转
题目描述
输入一个链表,反转链表后,输出新链表的表头。
解法:
1 | # -*- coding:utf-8 -*- |
链表中环的检测
题目描述
141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
题解
1 | # Definition for singly-linked list. |
有序链表合并
题目描述
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
Output: 1->1->2->3->4->4
题解
1 | # Definition for singly-linked list. |
删除链表中倒数第n个节点
题目描述
19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
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.
题解
1 | # Definition for singly-linked list. |
求链表的中间节点
题目描述
876. Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
题解
1 | class Solution(object): |