Flatten a binary tree to a fake "linked list" in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode.NoticeDon ' t forget to mark the left child of each node to null. Or you'll get time limit exceeded or Memory Limit exceeded.Example 1 1 2 / \ 2 5 => 3 / \ \ 3 4 6 4 5
This problem requires a comparison of the pointers to understand the position. Then the method is straightforward: the odd number of connected, even the even, and finally put together./** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode* Oddevenlist (listnode*head) { if(head==null| | head->next==null| | Head->next->next==null)retu
Note the restriction constraints in the INSERT and delete operations.classListNode {ListNode next; intVal; PublicListNode (intx) {val=x; }} Public classlinklist {PrivateListNode Curr =NULL; Public voidAppendtohead (intd) {ListNode tail=NewListNode (d); Tail.next=Curr; Curr=tail; } Public voidPrintappendtohead () { while(Curr! =NULL) {System.out.println (curr.val); Curr=Curr.next; } } Public voidDeleteintDthrowsException {//assume that the element you want to delete appears only once
translation给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点。假设这个链表是1234,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为124。OriginalWrite a function toDelete a node (except theTailinchA singly linkedList,givenOnly access to thatNode. Supposed theLinkedList is 1-2-3-4 andYou aregiven the ThirdNode withValue3, theLinkedListShould become1-2-4 AfterCalling your function.Code/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* Li
Title meaning: If there is a ring, return to the entry node.Idea: First judge have no ring, and then calculate the knot number of the ring, then P1 point to the head, P2 back to the node, p1, p2 meet for the entrance nodePS: or the idea of using the pointer spacing1 /**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: OneListN
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 if(Head==null)return false; -ListNode *
Enter a list of all the elements of the linked list after the list is reversed.1 /*2 Public class ListNode {3 int val;4 ListNode next = null;5 6 listnode (int val) {7 this.val = val;8 }9 }*/Ten Public classSolution { One PublicListNode reverselist (ListNode head) { A - if(NULL= = Head | |NULL==head.next) { - returnhead; the } - -ListNode lastnode =head; -ListNode Currnode =Head.next; +ListNode Prenode =Head.next
There is a good way to do this:1, reverse the two list, so that you can sequentially get the number from low to high2, synchronous traversal of two reverse chain list, add to create a new linked list, while focusing on rounding3. When two lists are traversed, follow the carry.4, the list of two reverse order is reversed again, adjusted backreturn result List PackageTT; Public classTest98 { Public classnode{ Public intvalue; PublicNode Next; PublicNo
Package Linklist;public class NodeGorgeous Split-line ********************************************************* ***************Package linklist;/** * **************** single-linked list with lead node and its implementation ********************* * * @author WL * */public class SINGLYLINKLISTL T t> {nodeJava data structure System--linked list (1): Single linked li
A long time ago practice, it is not very difficult to see. Packageproject;classnode{Private intId//Private is only accessible to this class of objects and methods. PrivateString name; PublicNode Next;//point to next class node PublicNode (intId,string name)//a method for constructing a parameter{ This. id=ID; This. name=name; } Public voidDisplayLink ()//Display node content{System.out.println ("ID:" +id+ "" + "Name:" +name); }}classlinklist{PrivateNode first; Publiclinklist () { F
Even if the world is deserted, there is always a person, he will be your believer. ----"The stars in the canoe"
First step: Create a nodeTemplate Const T GetData () {return m_data; }
Node
Step Two: Create a linked listTemplate Node int Getlistlen () {return m_ilen; }
void Insert (t data) {Node void display () {Node Private:int M_ilen; Node
... You can try the list in the main function ... int main (void) {list
Fourth Step:
When the list i
1#include"head.h"2 structStudent *creat ()3 {4 structStudent *head, *P1, *P2;//first open three struct pointers, *head, (as the return head pointer)5P1 = P2 = (structStudent *)malloc(LEN);6scanf_s ("%s%f", P1->num, N, p1->score);//first read the input information, according to read the information to Judge7head = NULL;//make the head pointer point to the null pointer first8n =0;//to count the number of members in a list9 while(strcmp (P1->num,"0") !=0)//according to the information read
2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.The quick pointer and the slow pointer begin to move in the head pointer, the fast pointer moves two steps at a time, the slow pointer moves one step at a time, until the meeting, after the meeting, the slow pointer points to the head pointer, and two pointers move backward one step. Until the meeting, where the meeting is the beginning of the c
/** Definition for singly-linked list. * struct ListNode {* int val; * struct ListNode *next; *};*/structlistnode* Reverselist (structlistnode*head) { if(!head| |! Head->next)returnHead; structListNode *cur=Head; structListNode *tail=Head; while(tail->next) Tail=tail->Next; while(cur!=tail) {Head=cur->Next; Cur->next=tail->Next; Tail->next=cur; Cur=Head; } returnhead;}There is a problem, leetcode given the list of test data should not include
1#include"head.h"2 structStudent *del_same_age (structStudent*head,intAge )3 {4 structStudent *p, *pt;intFind =0;5p = pt =head;6 while(P! =NULL)//when looping to the last node7 {8 9 if(P->stu_age = =Age )//if equalTen { Onefind++;//description found A if(p = =Head )//If found to be the first node - { -Head = p->Next; thep = p->next;//loop node move back -PT =p; - } - Else//If not the head node + { -Pt->next
#include There are already a, a, a, two linked lists, each of which includes the number of points, scores. The two linked lists are required to be merged in ascending order by the number of studies.
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.