Topic:Given A linked list, determine if it has a cycle in it.Ideas:For judging whether the chain list has a ring, the method is very simple, with two pointers, beginning to point to the head node, one is a fast pointer, two steps at a time, a slow pointer, one step at a time, when two pointers coincide with the existence of the ring.Fast first into the ring, afte
Topic: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 .Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤length of list.Ideas:Find the M node, reverse the pointer from node m to N, and then concatenate the flip-up/** Definition for singly-
$hero 2,new Hero (2,"Lu Junyi", "Jade Kylin")
Connect two objects,$hero->next= $hero 2
Traversing a linked list
Define a function Showheros (), parameter:$head Object
Defines a temporary variable $cur to store $head Objects
while loop, condition $cur->next not null
Print a bit
Pointer moves back,$cur = $cur->next
PHP Version:
Php/** * Hero class*/classhero{ Public $no; Public $name; Public $nickname
Title: Reverse list of nodes from m-n locationsFor example:Given1->2->3->4->5->null, m = 2 and n = 4,Return1->4->3->2->5->null.The nodes from the second to the fourth are reversed.where m and n satisfy the condition:1≤ m ≤ N ≤length of the list.Ideas:Still in reverse order, consider using a secondary space stack.The nodes of the m-n are sequentially placed into the stack and marked with the front and back two nodes adjacent to the stack node Pfirst an
.
After two traversal, the pointerpAThe pointer is located at node 4 in linked list B.pBIt will also reach the position of node 4 in the linked list A. the next traversal of the two pointers will encounter at node 9 (that is, the intersection node. At this time, the length of the two pointers is \ (l1 + l2 + l3 \). Th
Two-way linked listAndDouble-ended linked listAre different concepts.
A double-ended linked list refers to the head node of a linked list, which not only points to the first node but also the last node.
A two-way
remaining red lines
It is a Y and a Z. We know that the length of the multiple steps is m, that is, Y + z = m. The blue line is also M, that is, x + z = m.
So if we launch x = y directly, we will understand the second step.
If the fast node goes through a lot of circles, we can draw our own pictures and find that the model of the above figure is actually down.
Code Attached
/** * Definition for singly-linked
linked list
Define a function Showheros (), Parameter: $head object
Defines a temporary variable $cur to store $head objects
While loop, condition $cur->next NOT NULL
Print a bit
Pointer moves back, $cur = $cur->next
PHP Version:
"; $cur = $cur->next; }}} Linklistdemo::main ();
Java Edition:
Class hero{public int no; public String name; Public
from the intersection point to the node of the A-linked table head. (The so-called distance, is the head node walk several times can arrive)First look at the specific method of seeking:0, calculate the length of a linked list Lena1, calculate the length of the B-linked list
Write a program to find the node at which the intersection of the singly linked lists begins.For example, the following, linked lists:A: a1→a2 c1→c2→c3 B: b1→b2→b3Begin to intersect at node C1.Notes:
If The linked lists has no in
Write a program to find the node at which the intersection of the singly linked lists begins.For example, the following, linked lists:A: a1→a2 c1→c2→c3 B: b1→b2→b3Begin to intersect at node C1.Test instructions: To find the junction of two l
Write a program to find the node at which the intersection of the singly linked lists begins.For example, the following, linked lists:A: a1→a2 c1→c2→c3 B: b1→b2→b3Begin to intersect at node C1.Notes:
If The linked lists has no in
Topic:In a complex list, each node has a random reference in addition to a next reference to any node in the linked list or null, and the implementation function copies the linked list.Method One: HashMap storage node pair informationThe first step: (Traverse the original linked
Write a program to find the node at which the intersection of the singly linked lists begins.For example, the following, linked lists:A: a1→a2 c1→c2→c3 B: b1→b2→b3Begin to intersect at node C1.Notes:
If The linked lists has no in
Sort a linked list in O(n log n) time using constant space complexity.Common sorting methods are many, insert sort, choose Sort, heap sort, quick sort, bubble sort, merge sort, bucket sort, etc... Their time complexity is not the same, and here the topic limit time must be O (NLGN), meet the requirements of only fast sorting, merge sort, heap sorting, and according to the characteristics of single-
Sort a linked list using insertion sort.PS: Need to consider left as head and only a number, at this time left->==null, if RightCompare P->next->val and Right->val to avoid the need to record Prenode1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;
Given a sorted linked list, delete all duplicates such this each element appear only once.
For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.The idea of solving a problem is to traverse the list, if the elements of the 2 nodes are equal, delete the latter one, otherwise it will be shifted. It is important to note that you do not directly
The original title link is here: https://leetcode.com/problems/plus-one-linked-list/Topic:Given a non-negative number represented as a singly linked list of digits, plus one to the number.The digits is stored such, the most significant digit was at the head of the list.Examp
Given a singly linked list, determine if it is a palindrome.Example 1:Input:1->2output:falseExample 2:Input:1->2->2->1output:trueFollow up:Could do it in O (n) time and O (1) space?Please determine if a linked list is a palindrome list.Example 1:Input: 1->2 output: falseExam
/** * Definition for singly-linked list.
* struct ListNode {* int val;
* ListNode *next;
* ListNode (int x): Val (x), Next (NULL) {}};
*/void Mergelink (listnode* head,listnode* Mid, listnode* last) {listnode* head1=head;
listnode* End1 = mid;
listnode* rehead2 = mid->next;//records the end of the first list
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.