leetcode 旋转单链表】的更多相关文章

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g…
1.题目描述 反转一个单链表.链表节点结构如下: struct ListNode { int val; ListNode* next; }; 2.问题分析 特殊情况是输入的头结点是一个空的,或者只有一个头结点 3.代码实现 ListNode* reverseList( ListNode* head ) { if( head == NULL || head->next == NULL) return head; ListNode* pre = NULL; ListNode* next = NULL…
https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -> nxt -> nxtnxt -> .....  Here current = pre,nxt = pre->next, nxtnxt = nxt->next. After:   pre <- nxt     nxtnxt -> .....  Here current…
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. Note: Given n w…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设…
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only…
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Partition List \([82]\) Remove Duplicates from Sorted List II \([61]\) Rotate List \([19]\) Remove Nth Node From End of List LeetCode 单链表专题 <c++> \([2]\)…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 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.…
本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Solution { public: ListNode* reverseList(ListNode* head) { return reverseList_iteratively(head); //return reverseList_recursively(head);//递归方法leetcode超时 } /…
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 如何判断一个单链表中有环? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle…
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路(即1跑过,2也跑过),就是那段(环开始处,相遇处),那么指针2开始到环开始处的距离与head到指针相遇处是等长的喔~,那么再跑一次每次一步的就必定会相遇啦.画个图图好方便看~ (2)其实还有另一个直观的思路,就是指针1和2相遇后,p指向他们的next,在他们相遇处的next给置空,再跑一遍那个“找两…
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&…
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 说明:给定已排好序的单链表,去重! 1)此链表默认无头节点,head指针指向的是第一个节点 2)链表为空或者只有一…
题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool has…
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* swapPairs(List…
翻转一个单链表.这个题目听说很多次了,总感觉肯定不是什么难题. 现在真的有点好高骛远了!总感觉那种很难的算法题才是难题,这种题没必要做.其实眼高手低啊. 这种easy题,我都不能一遍ac,这遇到白板编程也是挂的节奏! 仔细分析,每次翻转一个,要记录被反转的这个的前后节点. 1 -> 2 -> 3 -> 4 用p记录当前要改变其next指针的节点.last 指向前一个节点.pre指向后一个节点. 初始化,last = NULL. struct ListNode* reverse(struc…
找到两个单链表的共同节点. 举例来说, 下面两个链表A和B: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 共同节点为c1. 分析: 共同节点距离A,B的起点headA, headB的距离差为定值, 等于它们的各自总长的差值, 我们只需要求出这个差值, 把两个链表的头移动到距离c1相等距离的起点处即可. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val;…
1.问题描述 Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 给定一个单链表和一个数值,删除单链表中数据域等于该数值的节点. 2.问题分析 遍历一次链表,找到数据域等…
1.将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. (可以参照第2的merge2List实现) 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/merge-two-sorted-lists 易错点: (1)没考虑其中一个是空链表情况 (2)往前插入和往后插入的情况处…
Sort a linked list in O(n log n) time using constant space complexity. 常见排序方法有很多,插入排序,选择排序,堆排序,快速排序,冒泡排序,归并排序,桶排序等等..它们的时间复杂度不尽相同,而这里题目限定了时间必须为O(nlgn),符合要求只有快速排序,归并排序,堆排序,而根据单链表的特点,最适于用归并排序.代码如下: C++ 解法一: class Solution { public: ListNode* sortList(L…
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.DEFINITIONCircular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the…
给定一个单链表,设计一个算法实现链表向右旋转 K 个位置. 举例: 给定 1->2->3->4->5->6->NULL, K=3 则     4->5->6->1->2->3->NULL 分析: 1. 这道题目的关键在于找到 尾节点和旋转后新链表的尾节点. 假设是 tail, new->tail. 然后只要进行 tail->next  = head; head = new_tail->next; new_tail-&…
本文描述了LeetCode 148题 sort-list 的解法. 题目描述如下: Sort a linked list in O(n log n) time using constant space complexity. 题目要求我们在O(n log n)时间复杂度下完成对单链表的排序,我们知道平均时间复杂度为O(n log n)的排序方法有快速排序.归并排序和堆排序.而一般是用数组来实现二叉堆,当然可以用二叉树来实现,但是这么做太麻烦,还得花费额外的空间构建二叉树,于是不采用堆排序. 故本…
在LeetCode中看到判断回文的程序:https://leetcode.com/problems/palindrome-linked-list/ 里面用单链表来存储数据,先反转前半部分的单链表,然后分别从 表头 和 中间链表位置处 开始比较元素. 反转单链表的代码如下: 1 private ListNode reverseList(ListNode head, int length){ 2 if(head == null) 3 return null; 4 ListNode currentNo…
今天做leetcode,遇到了单链表反转.研究了半天还搞的不是太懂,先做个笔记吧 参考:http://blog.csdn.net/guyuealian/article/details/51119499  https://www.cnblogs.com/hiver/p/7008112.html class Node { public String value; public Node next; public Node(String value) { this.value = value; } }…
Status: AcceptedRuntime: 66 ms 题意:根据给出的单链表,用O(nlogn)的时间复杂度来排序.由时间复杂度想到快排.归并这两种排序.本次用的是归并排序.递归将链表的规模不断二分到只剩下1或2个元素为止,这也是递归出口,一旦出现这两种情况就可以返回.这里有个问题,链表也能二分?可以的,只是麻烦了些,用两个指针可以实现找到中点.本次代码没有详细分析具体的复杂度,但确实是归并. 注意:要考虑空链表,单个元素的链表,以及多个元素的链表. LeetCode的代码: /** *…
LeetCode:奇偶链表[328] 题目描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数. 示例 1: 输入: 1->2->3->4->5->NULL 输出: 1->3->5->2->4->NULL 示例 2: 输入: 2->1-…
LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baidu_31657889/ csdn:https://blog.csdn.net/abcgkj/ github:https://github.com/aimi-cn/AILearners 一.引子 这是由LeetCode官方推出的的经典面试题目清单~ 这个模块对应的是探索的初级算法~旨在帮助入门…
Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式截然不同 什么是单链表 单链表 最简单的形式就是由多个节点的集合共同构成一个线性序列.每个节点存储一个对象的引用,这个引用指向序列中的一个元素,即存储指向列表的下一个节点. 其实,上面的术语用生活中的大白话来解释,就是我们现在有三个人--我.你.他.当我用手指指向你,你用手指指向他,这样就形成了一个…
地址 https://www.acwing.com/solution/LeetCode/content/7132/ 题目描述给你一个单链表的引用结点 head.链表中每个结点的值不是 0 就是 1.已知此链表是一个整数数字的二进制表示形式. 请你返回该链表所表示数字的 十进制值 . 示例 : ->->->null 输入:head = [,,] 输出: 解释:二进制数 () 转化为十进制数 () 示例 : 输入:head = [] 输出: 示例 : 输入:head = [] 输出: 示例…