The link list has loops, returns True, otherwise returns falseIdeas: Two pointers, a fast and slow, can meet the ring, for the empty ringPS: Many lists of topics: You can use this idea1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: One BOOLHascycle (ListNode *head) { A
Palindrome Linked ListImplement a function to check if a linked list is a palindrome.ExampleGiven 1->2->1 , return TrueChallengeCould do it in O (n) time and O (1) space?////1\find out the medium index of Linked list2\ reverse the right part of linked list in place3\compare their value;/*** Definition for singly-linked list
A linked list is given such this each node contains an additional random pointer which could point to all node in the list or null.Return a deep copy of the list.Ideas:First, according to the normal order, to get a copy of the linked list, while using a hashmap to establish the corresponding relationship between the old and new nodes, and then add the random poin
Given a singly linked list l:l0→l1→ ... →ln-1→lnReorder It to:l0→ln→l1→ln-1→l2→ln-2→ ...ExampleGiven 1->2->3->4->null , reorder it to 1->4->2->3->null .ChallengeCan do this in-place without altering the nodes ' values?/*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; * } * } */ Public classSolution {/** * @paramhead:the head of linked
Given a linked list and both values V1 and v2. Swap the nodes in the linked list with values V1 and v2. It ' s guaranteed there is no duplicate values in the linked list. IF v1 or V2 does not exist in the given linked list, does nothing.NoticeYou should swaps the nodes with values V1 and v2. Do not directly swap the va
Sort a linked list in O(n log n) time using constant space complexity.ExampleGiven 1-3->2->null, sort it to 1->2->3->null./*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; * } * } */ Public classSolution {/** * @paramhead:the head of linked list. * @return: You should return the head of the sorted linked
Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.ExampleGiven 1->2->3->3->4->4->5 , return 1->2->5 .Given 1->1->1->2->3 , return 2->3 ./*** Definition for ListNode * public class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * next = NULL; * } * } */ Public classSolution {/** * @paramListNode Head is the head of the linked
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.