New Ket Net Algorithm learning notes-linked list

Source: Internet
Author: User

For a linked list, we need to use a specific threshold to complete its differentiation, so that the node is less than or equal to the value of the previous, the node is greater than the value of the next, while ensuring that the two types of nodes within the position of the same relationship.

Given a list of head node heads, at the same time given a threshold Val, return a linked list so that the node is less than or equal to its first, greater than or equal to its after, guaranteed node value is not duplicated. Test examples:

{1,4,2,5},3
{1,2,4,5}

Two head nodes are established separately, and two tail nodes are established, which ensures that the head pointer points to two sub-blocks of the head node, the tail node expands the element, and then stitching at the end, paying attention to returning the next node of the head node.

/* struct ListNode {
int Val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}
};*/
Class Divide {
Public
listnode* Listdivide (listnode* head, int val) {
Write code here
listnode* le=new listnode (0);
listnode* gr=new listnode (0);
ListNode *cur_le=le,*cur_gr=gr;
for (; head!=null;head=head->next) {
if (head->val<=val) {
cur_le->next=head;
cur_le=cur_le->next;
}
else{
cur_gr->next=head;
cur_gr=cur_gr->next;
}
}
cur_le->next=gr->next;
cur_gr->next=null;
Return le->next;
}

};


There are two ascending linked lists, and there are no duplicate elements in the list. Please design an efficient algorithm to print the common values section of the two lists.

Given the two-linked list of head pointers Heada and headb, return a vector with elements that are the public part of the two linked list. Make sure to return the ascending order of the array. The number of elements in the two list is less than or equal to 500. Ensure there must be a common value test sample:

{1,2,3,4,5,6,7},{2,4,6,8,10}
return: [2.4.6]

Two head pointer, small shift, same time move back and store node value until a chain is null;/*
struct ListNode {
int Val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}
};*/
Class Common {
Public
Vector<int> findcommonparts (listnode* heada, listnode* headb) {
Write code here
vector<int> result;
if (Heada = = NULL | | HEADB = = NULL) {
return result;
}
while (Heada! = null&&headb!=null) {

if (heada->valHeada = Heada->next;
}
else if (heada->val = = headb->val) {
Result.push_back (Heada->val);
Heada = Heada->next;
HEADB = headb->next;
}
else{
HEADB = headb->next;
}

}
return result;
}

};


5.7

There is a single linked list, please design an algorithm, so that each k nodes in reverse order, if the last not enough K nodes a group, then not adjust the last few nodes. For example the list 1->2->3->4->5->6->7->8->null,k=3 this example. After adjustment for, 3->2->1->6->5->4->7->8->null. Because of k==3, the reverse is reversed between each of the three nodes, but the 7,8 is not adjusted because only two nodes are not enough.

Given the head of a single-linked list, given the K value, the head pointer of the linked list is returned in reverse order.

My code (said run timeout, no reason found).

/*
struct ListNode {
int Val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}
};*/
Class Kinverse {
Public
listnode* Inverse (listnode* head, int k) {
Write code here
if (head = = NULL | | k ==1) {
return head;
}
ListNode *resulthead = new ListNode (0);
ListNode * Temphead = Resulthead;
Stack<listnode*> Kstack;
BOOL first = FALSE;
int count = 0;
while (Head!=null) {
Kstack.push (head);

count++;
if (count>=k) {
while (count>0) {//Reverse
listnode* t = kstack.top ();
Kstack.pop ();
Temphead->next = t;
Temphead = Temphead->next;
count--;
}
}
Head = Head->next;
}
Temphead->next = NULL;
if (count>0&&count<k) {
while (count>0) {
listnode* t = kstack.top ();
Kstack.pop ();
T->next = Temphead->next; Head interpolation method. The continuation of the discontent K
Temphead->next = t;
count--;
}
}
return resulthead->next;
}
};


Code for someone's home:

/* struct ListNode {int val;
    struct ListNode *next;
        ListNode (int x): Val (x), Next (NULL) {}};*/class Kinverse {public:listnode* inverse (listnode* head, int k) {
        Stack<listnode *> Mystack;
        ListNode *pre=null;
        ListNode *behind=head;
            while (behind!=null) {int count=0;
                while (behind!=null&&count<k) {//In the stack, reverse mystack.push (Behind);
                behind=behind->next;
            count++;
            } if (count<k) break;
                while (!mystack.empty ()) {ListNode *temp=mystack.top ();
                    if (pre==null) {pre=temp;
                Head=pre;
                    } else{pre->next=temp;
                pre=pre->next;
            } mystack.pop ();  } pre->next=behind;
       Pre stitching the contents of the remaining original list} return head; }
};

Now there is a single linked list. Each node in the list holds an integer and, given a value, Val, deletes all nodes that are equal to Val.

Given the head of a single-linked list, given a value Val, return the head node of the purged list to ensure that there are other values in the list that are not equal to the value. Make sure that the other elements are in relative order. Test examples:

{1,2,3,4,3,2,1},2
{1,3,4,3,1}

node is passed without a head node, and head points directly to the first node. So you can increase the head node so that all nodes operate the same way.

/*
struct ListNode {
int Val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}
};*/
Class ClearValue {
Public
listnode* Clear (listnode* head, int val) {
Write code here
listnode* Listhead = (listnode*) malloc (sizeof (ListNode));
Listhead->next = head;
ListNode *pre = Listhead;
while (Head!=null) {
if (head->val==val) {
ListNode *t = head;
Head = Head->next;
Pre->next = head;
Free (t);
}
else{
Head = head->next;
Pre = Pre->next;
}
}
Return listhead->next;
}
};


Please write a function to check if the list is a palindrome.

Given a list of listnode* Phead, return a bool that indicates whether the linked list is a palindrome. Test examples:

{1,2,3,2,1}
Return: True
{1,2,3,2,3}
Return: False
Put the linked list in a stack, and then from the stack head a popup and the original linked list to compare, if you do not want to return false in the comparison process
Otherwise returns true

/*
struct ListNode {
int Val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}
};*/
Class Palindrome {
Public
BOOL Ispalindrome (listnode* phead) {
Write code here
Stack<listnode*> Restack;
listnode* temp = Phead;
while (Temp!=null) {
Restack.push (temp);
temp = temp->next;
}
while (Phead!=null) {
ListNode *t = Restack.top ();
Restack.pop ();
if (t->val! = phead->val) {
return false;
}
Phead = Phead->next;
}
return true;
}
};


5.11

Enter a complex list with node values in each node, and two pointers, one pointing to the next node, and the other a special pointer to any node.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.