
LinkedList
文章平均质量分 67
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Swapping Nodes in a Linked List
You are given theheadof a linked list, and an integerk. Returnthe head of the linked list afterswappingthe values of thekthnode from the beginning and thekthnode from the end (the list is1-indexed). Example 1: Input: head = [1,2,3,4,5], k ...原创 2022-05-02 14:11:57 · 481 阅读 · 0 评论 -
Delete the Middle Node of a Linked List
You are given theheadof a linked list.Deletethemiddle node, and returntheheadof the modified linked list. Themiddle nodeof a linked list of sizenis the⌊n / 2⌋thnode from thestartusing0-based indexing, where⌊x⌋denotes the largest integer...原创 2021-12-05 12:49:56 · 214 阅读 · 0 评论 -
Reverse Nodes in Even Length Groups
You are given theheadof a linked list. The nodes in the linked list aresequentiallyassigned tonon-emptygroups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). Thelengthof a group is the number of nodes assigned to it. In o...原创 2021-11-14 14:50:07 · 318 阅读 · 0 评论 -
Insert into a Sorted Circular Linked List
Given a node from aCircular Linked Listwhich is sorted in ascending order,write a function to insert a valueinsertValinto the list such that it remains asorted circular list. The given node can be a reference toanysingle node in the list, and may n...原创 2020-06-13 11:45:20 · 436 阅读 · 0 评论 -
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: Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanatio...原创 2020-05-10 02:05:02 · 121 阅读 · 0 评论 -
Linked List Components
We are givenhead,the head node of a linked list containingunique integer values. We are also given the listG, a subset of the values in the linked list. Return the number of connected components inG, where two values are connected if they appear con...原创 2020-05-09 02:57:26 · 176 阅读 · 0 评论 -
Flatten a Multilevel Double Linked List
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists ma...原创 2020-02-24 04:50:34 · 156 阅读 · 0 评论 -
First Unique Number in Data Stream
Given a continuous stream of data, write a function that returns the first unique number (including the last number) when the terminating number arrives. If the unique number is not found, return-1. ...原创 2020-01-02 14:02:14 · 658 阅读 · 1 评论 -
Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() --...原创 2014-12-30 14:45:33 · 541 阅读 · 0 评论 -
Convert Binary tree to Linked Lists by Depth
Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D, you'll have D linked lists). Example Example 1: Input: {...原创 2019-10-24 11:31:03 · 338 阅读 · 1 评论 -
Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:首先建立一对一的关系,用hashmap保持住,然后再从头走到尾...原创 2014-12-15 12:38:42 · 465 阅读 · 0 评论 -
Add Two Numbers
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 l...原创 2014-12-01 07:16:46 · 455 阅读 · 0 评论 -
Add Two Numbers II
You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2016-10-29 09:44:13 · 398 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear onlyonce. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 思路:...原创 2013-12-30 13:28:39 · 528 阅读 · 0 评论 -
Insertion Sort Integer Array & Insertion Sort Linked List
Sort Integer Array using Insertion sort. //******************************************************************************************************** /* Insertion Sort 原理:就是前面的sort部分全部是相对值,从后面拿一个元素,然...原创 2016-07-29 13:26:12 · 465 阅读 · 0 评论 -
Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is t...原创 2018-12-24 07:08:02 · 355 阅读 · 0 评论 -
Linked List Components
We are given head, the head node of a linked list containing unique integer values. We are also given the list G, a subset of the values in the linked list. Return the number of connected components...原创 2018-12-25 08:27:33 · 275 阅读 · 0 评论 -
Merge Sort Array and Merge Sort Linked List
Merge Sort Array: 看完stanford的 CS106B的video,https://www.youtube.com/watch?v=LlNawf0JeF0&list=PLnfg8b9vdpLn9exZweTJx44CII1bYczuk&index=55醍醐灌顶; public class Solution { /** * @param A:...原创 2018-12-26 09:02:40 · 830 阅读 · 0 评论 -
纪念一下第二个assignment 100分
感悟就是:坚持,才能从good到great。精益求精就是要不断打磨产品。 Princeton的课就是好,一个作业可以牵扯到很多算法。复习了shuffle算法和Resevoir Sampling算法,还有linkedin,array implement deque,iterator的用法,确实不错的课程,经典就是经典!刷题不在乎刷题数目多少,而在于刷背后知识点的深度和广度。加油!我觉得我刷完A...原创 2019-03-29 08:54:32 · 211 阅读 · 0 评论 -
Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Time: O(N) Space: O(N) /** * Definiti...原创 2016-07-11 14:38:36 · 236 阅读 · 0 评论 -
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can you solve it without using extra space? 思路:two pointers 问题。用快慢指针来运算。fast指针速度是slow指针的2倍,...原创 2013-12-31 13:38:07 · 525 阅读 · 0 评论 -
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked listkat a time and return its modified list. If the number of nodes is not a multiple ofkthen left-out nodes in the end should remain as it is.原创 2014-12-20 06:03:55 · 400 阅读 · 0 评论 -
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. 思路: 实际上考察的是mergesort里面的merge一步,很简单直接的merge,注意其中一个没有了的情况。一...原创 2014-01-27 13:56:37 · 543 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:jame bond 的思路,找mid的前一个点,然后两边分开找;这里只传递一个参数; /** * Definition for singly-linked list....原创 2016-07-25 12:47:40 · 342 阅读 · 0 评论 -
Reverse Linked List
Reverse a singly linked list. Iterative 解法: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ c...原创 2016-06-08 10:50:13 · 410 阅读 · 0 评论 -
Delete a node, only give access to that node.
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node wit...原创 2016-06-12 00:15:26 · 257 阅读 · 0 评论 -
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: A: a1 → a2 ↘ ...原创 2014-11-28 16:04:58 · 424 阅读 · 0 评论 -
Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Not...原创 2014-12-04 13:40:32 · 431 阅读 · 0 评论 -
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You m原创 2013-12-31 09:24:45 · 561 阅读 · 0 评论 -
Design Phone Directory
Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone.check: Check if a number is available or not.release: Recycle or release a原创 2016-09-26 08:12:06 · 847 阅读 · 0 评论 -
Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2...原创 2014-02-10 08:21:12 · 486 阅读 · 0 评论 -
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 p原创 2016-07-25 08:40:58 · 295 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 思路:首先算...原创 2014-01-12 15:23:39 · 469 阅读 · 0 评论 -
Plus One Linked List
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:原创 2016-07-20 06:08:00 · 662 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->原创 2013-12-30 16:07:07 · 637 阅读 · 0 评论 -
Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l原创 2014-01-16 13:02:19 · 441 阅读 · 0 评论 -
Remove Linked List Elements
Remove all elements from a linked list of integers that have valueval. ExampleGiven:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,val= 6Return:1 --> 2 --> 3 --> 4 --> 5 ...原创 2016-06-10 13:45:40 · 280 阅读 · 0 评论 -
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? 思路:同向双指针;一个fast 一个slow指针跑,能meet说明有cycle,遇到null证明没有。 注意,一切都要以fast 为判断准则,因为跑得快。 /** ...原创 2013-12-31 13:26:37 · 526 阅读 · 0 评论