linked

*92. Reverse Linked List II (follow up questions)

倖福魔咒の 提交于 2020-04-02 05:56:25
Reverse a linked list from position m to n . Do it in one-pass and in-place Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL FIrstly, I wanna use stack but need cosume space Finally, I use in-plcae method 思路:reference: http://bangbingsyb.blogspot.com/2014/11/leetcode-reverse-linked-list-ii.html 反转整个链表的变种,指定了起点和终点。由于m=1时会变动头节点,所以加入一个dummy头节点 1. 找到原链表中第m-1个节点start:反转后的部分将接回改节点后。 从dummy开始移动m-1步 D->1->2->3->4->5->NULL | st 2. 将从p = start->next开始,长度为L = n-m+1的部分链表反转。 __________ | | | V D->1->2<-3<-4 5->NULL | | | st p h0 3. 最后接回 _______

LeetCode 328. Odd Even Linked List

只谈情不闲聊 提交于 2020-03-22 22:21:00
题目链接: https://leetcode.com/problems/odd-even-linked-list/ 328. Odd Even Linked List My Submissions Question Total Accepted: 13612 Total Submissions: 35990 Difficulty: Easy Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. Example: Given 1->2->3->4->5->NULL , return 1->3->5->2->4->NULL . Note: The relative order inside both the even and odd groups should remain as

leetcode-Odd Even Linked List

纵饮孤独 提交于 2020-03-22 22:17:52
Odd Even Linked List Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. Example: Given 1->2->3->4->5->NULL , return 1->3->5->2->4->NULL 题意:奇数,偶数编号节点分开, 两个指针一个指向奇数开始,一个指向偶数开始,每次把first的下一个节点指向second的下一个节点。 一个关键点是 偶数个节点最后一个是偶数,奇数节点最后一个是奇数,那么用一个count进行奇数,如果最后是奇数,那么要用second指向head2 如果是偶数,first指向head2. 最后返回head1。(在纸上画一下就明白了)具体看代码 1 /** 2 * Definition for singly-linked list

Reverse Linked List

[亡魂溺海] 提交于 2020-03-22 22:17:00
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? /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { ListNode *pre = new ListNode(0), *cur = head; pre -> next = head; while (cur && cur -> next) { ListNode* temp = pre -> next; pre -> next = cur -> next; cur -> next = cur -> next -> next; pre -> next

leetcode 237 Delete Node in a Linked List

对着背影说爱祢 提交于 2020-03-22 22:16:46
题目链接 : https://leetcode.com/problems/delete-node-in-a-linked-list/ 思路分析 :题目要求在链表中删除给定的node,该node不为链表中的tail。因为没有node的前一个结点,所以想要直接从linked list中直接删除结点node是不可能的。 根据题目信息,可以通过将node.next结点的值赋给node,并删除node.next结点,则效果与直接删除node结点相同。 代码如下: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node.val = node.next.val node.next = node.next.next 来源: https://www.cnblogs.com/tallisHe/p/5313367

LeetCode 237 Delete Node in a Linked List 解题报告

孤街浪徒 提交于 2020-03-22 22:16:29
题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 题目分析及思路 要求写一个函数,删除一个单链表中的一个结点(除了最后一个)。函数不需要返回值,只需要原地修改给定删除结点。我们可以将要删除结点的两个属性用该结点的下一个结点的两个属性来替代。 python代码 # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def deleteNode(self, node): """ :type node: ListNode :rtype: void Do not return anything, modify node in-place instead. """ node.val = node.next.val node.next = node.next.next 来源: https://www.cnblogs.com/yao1996/p/10660933.html

206. Reverse Linked List

廉价感情. 提交于 2020-03-22 22:02:55
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? public ListNode reverseList(ListNode head) { if(head==null) return null; if (head.next == null) return head; ListNode node = reverseList(head.next); head.next.next=head; head.next=null; return node; }    来源: https://www.cnblogs.com/clarencezzh/p/10957073.html

[LeetCode]77. Reverse Linked List反转链表

烂漫一生 提交于 2020-03-05 14:00:47
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? Subscribe to see which companies asked this question 解法1:一个最简单的办法就是借助栈的后进先出功能,先扫描一遍链表保存每个节点的值,然后再从头到尾遍历,将栈中元素值一一赋给链表节点。时空复杂度都是O(n)。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNode* head) { stack<int> elem; ListNode* curr = head; while(curr != NULL) { elem.push(curr->val); curr = curr->next; } curr

LeetCode-206. Reverse Linked List(反转链表)

不羁的心 提交于 2020-03-03 08:07:06
反转链表 Reverse Linked List 解 方法一:迭代 /** https://leetcode-cn.com/problems/reverse-linked-list/ * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public : ListNode * reverseList ( ListNode * head ) { ListNode * cur = NULL ; while ( head ) { ListNode * temp = cur ; cur = head ; head = head - > next ; cur - > next = temp ; } return cur ; } } ; 方法二:递归 /** https://leetcode-cn.com/problems/reverse-linked-list/ * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *

LeetCode 160.Intersection of Two Linked Lists.求两个链表的交点

走远了吗. 提交于 2020-03-02 11:46:00
题目: 已知链表A的头节点指针headA,链表B的头节点指针headB,连个链表相交,求两链表交点对应的节点。 要求: 1、如果两个链表没有交点,则返回NULL 2、在求交点的过程中,不可以破坏链表的结构或者修改链表的数据域。 3、可以确保传入的链表A与链表B没有任何环 4、*实现算法尽可能使用时间复杂度O(n),控件复杂度O(1) 思路一: 遍历链表A,将A中节点对应的指针(地址),插入set 遍历链表B,将B中节点对应的指针(地址),在set中查找, 发现在set中的第一个节点指针,及时两个链表的交点。 代码: 使用set的方法,这种方法关键是理解,set中存储的是指针! //使用set的方法 #include < stdio . h > #include < iostream > #include < set > using namespace std ; struct ListNode { int val ; ListNode * next ; ListNode ( int x ) : val ( x ) , next ( NULL ) { } } ; //只是定义的时候表名它是指针类型 //传的参数可以直接在方法里面用,以前还在一直纠结 ListNode * getIntersectionNode ( ListNode * a , ListNode * b ) { set