Linked List - leetcode

138. Copy List with Random Pointer

//不从head走 前面加一个dummy node 从dummy走先连head 只需记录当前节点
//这样就不需要考虑是先new node还是先找联系
//扫两遍 先建立节点和next 利用map<node*, node*>old2new 来找random

时间: 2024-11-05 18:42:39

Linked List - leetcode的相关文章

Flatten Binary Tree to Linked List leetcode java

题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

Palindrome Linked List Leetcode

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? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

Intersection of Two Linked Lists leetcode

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 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

Flatten Binary Tree to Linked List &lt;leetcode&gt;

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 算法:该题用的是递归,只要考虑当前节点,思路如下,设当前节点为root,把root的左节点的最右节点的右子树指向root的右子树,然后把root的右指针指向root的左子树,root的做指针置空,下一个节点处理r

Flatten Binary Tree to Linked List ——LeetCode

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题目大意:给一个二叉树,将它转为一个list,转换后的序列应该是先序遍历,list由这棵树的右孩子表示. 解题思路:递归的处理左子树,然后处理右子树,将左孩子的右孩子置为当前节点的右孩子,然后将当前节点的右孩子置

Reverse Linked List Leetcode

真的是太久太久没有刷题了....那天阿里面到这么简单的题目发现自己都写不利索了...哭瞎... Reverse a singly linked list. 可以用递归和非递归的方法. 递归: 主要思想是定义一个nextNode,用nextNode作为尾部直接连前面一个. public class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { retu

Plus One Linked List -- LeetCode

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 思路:递归. 1 /** 2 *

876. Middle of the Linked List - LeetCode

Question 876.?Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完链表,另一个恰好在中间 Java实现: public ListNode middleNode(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null) { fast = fast.next; i

(Easy) Delete Node in a Linked List - LeetCode

Description: 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: Input: head = [4,5,1,9], node = 5 Output: [4,1,9]

(Easy) Reverse linked list LeetCode

Description: 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? Accepted 661,364