LinkedList
yuanhisn
这个作者很懒,什么都没留下…
展开
-
LeetCode 142 - Linked List Cycle II
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? 算法参见Wekipedia. 1. 假设圈的周长λ,相遇的时候乌龟走的路程:μ + k,...原创 2015-01-07 13:37:43 · 51 阅读 · 0 评论 -
Convert a Binary Tree to Doubly Linked List
Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes...原创 2015-03-22 05:11:40 · 58 阅读 · 0 评论 -
LeetCode 25 - Reverse Nodes in k-Group
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...原创 2015-04-24 14:16:34 · 43 阅读 · 0 评论 -
LeetCode 203 - Remove Linked List Elements
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 p...原创 2015-05-01 02:03:58 · 54 阅读 · 0 评论 -
LeetCode 83 - Remove Duplicates from Sorted List
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. ...原创 2015-06-25 12:28:28 · 44 阅读 · 0 评论 -
LeetCode 82 - 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->...原创 2015-06-25 14:40:56 · 37 阅读 · 0 评论 -
LeetCode 141 - 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? Solution 1: public boolean hasCycle(ListNode head) { ListNode walker = head, r...原创 2015-07-21 11:44:35 · 55 阅读 · 0 评论 -
Convert a BST to a sorted circular doubly-linked list
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. If the problem stat...原创 2015-03-22 05:11:18 · 82 阅读 · 0 评论 -
Remove duplicates from an unsorted linked list
Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed? Solution: public ListNode removeDu...原创 2015-03-04 14:47:15 · 56 阅读 · 0 评论 -
LeetCode 19 - 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 fro...原创 2015-01-22 06:44:32 · 45 阅读 · 0 评论 -
Sum of Two Linked Lists - 1
Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers. Example 1 Input: First List: 5->6-&g...原创 2015-01-29 05:53:35 · 59 阅读 · 0 评论 -
Sum of Two Linked Lists - 2
Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. It is not allowed to modify th...原创 2015-01-29 05:56:02 · 65 阅读 · 0 评论 -
LeetCode 109 - 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. private ListNode h; public TreeNode sortedListToBST(ListNode head) { if(head == n...原创 2015-02-06 14:50:06 · 47 阅读 · 0 评论 -
LeetCode 138 - 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. Solution 1: 这种方法很直观,先遍历链表一次,拷贝n...原创 2015-02-15 00:35:14 · 52 阅读 · 0 评论 -
链表排序 - Sort LinkedList
这篇文章分析一下链表的各种排序方法。 以下排序算法的正确性都可以在LeetCode的链表排序这一题检测。本文用到的链表结构如下(排序算法都是传入链表头指针作为参数,返回排序后的头指针) struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ...原创 2015-02-16 02:10:19 · 132 阅读 · 0 评论 -
LeetCode 61 - 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. Solution:...原创 2015-02-25 02:59:18 · 37 阅读 · 0 评论 -
LeetCode 148 - Sort List
Sort a linked list in O(n log n) time using constant space complexity. Solution: Use merge sort. private ListNode h; //保存当前要访问的节点(不一定是链表的头部) public ListNode sortList(ListNode head) { if(hea...原创 2015-02-25 02:59:37 · 42 阅读 · 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: A: a1 → a2 ↘ ...原创 2015-08-03 23:25:19 · 84 阅读 · 0 评论