Offer-js-List of swords and fingers

Source: Internet
Author: User

(1) Enter a list to print the value of each node of the list from the end of the head.

Idea: Using an array to receive the printed list, the structure of the list is given.

/*function ListNode (x) {

This.val = x;

This.next = null;

}*/

function Printlistfromtailtohead (head)

{

var arr=[];

while (head) {

Arr.unshift (Head.val);

Head=head.next;

}

return arr;

}

(2)

Enter a list to output the last K nodes in the linked list .

Function Findkthtotail (head, K)

{

var arr=[];

while (head)

{

Arr.push (head);

Head=head.next;

}

if (arr.length==0 | | k>arr.length) {return false;}

return arr[arr.length-k];

}

(3) Enter a list of all the elements of the linked list after the list is inverted.

Idea: Create a new head node, traverse the original list, and insert each node into the new linked list with a head node. Finally, the newly created linked list is the inverted list.

function Reverselist (phead)

{

var pre = null;

var next = null;

while (Phead) {

Next = Phead.next;

Phead.next = pre;

Pre = Phead;

Phead = Next;

}

return pre;

}

(4) Input two monotonically increasing list, output two linked list of the linked list, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.

function Merge (pHead1, pHead2)
{
if (PHead1 = = NULL | | pHead2 = = NULL) {
return PHead1 | | PHead2;
}

var head = null;

if (Phead1.val < Phead2.val) {
head = PHead1;
Head.next = Merge (Phead2,phead1.next)
}
else {
head = pHead2;
Head.next = Merge (PHead1, Phead2.next);
}
return head;

}

(5) Enter a complex list (node value in each node, and two pointers, one point to the next node, another special pointer to any node), and the result is the head of the complex linked list after replication.

递归思想:把大问题转化若干子问题 此题转化为一个头结点和除去头结点剩余部分,剩余部分操作和原问题一致

function Randomlistnode (x) {
This.label = x;
This.next = null;
This.random = null;
}
function Clone (phead)
{
if (!phead) {
return null;
}
var clonehead=new randomlistnode (Phead.label);
Clonehead.label=phead.label;
Clonehead.random=phead.random;

Clonehead.next=clone (Phead.next);
return clonehead;
}

Offer-js-List of swords and fingers

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.