LeetCode:链表
在深圳搬砖
一入IT深似海,搬起砖来特别嗨
展开
-
LeetCode:234. 回文链表
一、问题描述 请判断一个链表是否为回文链表。 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 二、解题思路 将链表分为两段,并且把后面一段进行倒置,然后再比较前后两段是否相同即可。最简单的方法就是先计算链表的长度,然后将前面一半用头插法的办法进行倒置,最后再比较,但是由于需要的是一半这个特殊的位...原创 2019-09-24 17:05:20 · 73 阅读 · 0 评论 -
LeetCode:92. Reverse Linked List II
一、问题介绍 Reverse a linked list from positionmton. Do it in one-pass. Note:1 ≤m≤n≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->...原创 2019-09-17 18:49:27 · 102 阅读 · 0 评论 -
单链表:从头到尾打印链表
解题思路: 第一种方法:逆置单链表,然后输出; 第二种方法:利用栈的特性(先进后出),从头部入栈,然后出栈即可; 下面着重实现第二种方法。 代码实现 /** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * va...原创 2019-09-18 12:50:09 · 293 阅读 · 0 评论 -
LeetCode:237. Delete Node in a Linked List
一、问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list --head =[4,5,1,9], which looks like following: Example 1...原创 2019-09-19 17:33:59 · 78 阅读 · 0 评论 -
LeetCode:19. Remove Nth Node From End of List
一、问题描述 Given a linked list, remove then-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 th...原创 2019-09-19 18:54:30 · 70 阅读 · 0 评论 -
LeetCode:160. Intersection of Two Linked Lists
一、问题描述 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. 二、思路分析 这道题跟删除尾部第...原创 2019-09-19 20:25:42 · 78 阅读 · 0 评论 -
LeetCode:21. Merge Two Sorted Lists
一、问题描述 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 Ou...原创 2019-09-19 20:56:46 · 85 阅读 · 0 评论 -
LeetCode:23. Merge k Sorted Lists
一、问题描述 Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->...原创 2019-09-23 20:15:36 · 76 阅读 · 0 评论 -
LeetCode:148. Sort List
一、问题描述 Sort a linked list inO(nlogn) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Out...原创 2019-09-24 11:34:54 · 87 阅读 · 0 评论 -
LeetCode:138. 复制带随机指针的链表
一、问题描述 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 二、解题思路 第一种:用hashmap,遍历链表然后把它存到 HashMap 中,val 作为 key,Node 作为 value。遍历第二遍链表,将之前生成的节点取出来,更新它们的 next 和 random 指针。查找复杂度为 O(1) ,这样可以把总时间复杂度降到 O(n) ,由于...原创 2019-09-24 16:00:04 · 74 阅读 · 0 评论 -
LeetCode:328. 奇偶链表
一、问题描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。 请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。 示例 1: 输入: 1->2->3->4->5->NULL 输出: 1->3->...原创 2019-09-24 16:52:41 · 86 阅读 · 0 评论 -
LeetCode:206. Reverse Linked List
一、问题描述 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 二、代码实现 /** * Definition for singly-linked list. * struct Lis...原创 2019-09-17 16:57:47 · 97 阅读 · 0 评论