“This is the fifth day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

preface

This year, the last wave of moon more activities, do not get a participation award, that is a pity, so prepared a relatively simple algorithm seven tianle. It just so happens that I need to improve my algorithm.

Topic describes

Topic links: sorted circular linked lists

For the title description, I mainly use the screenshot leetcode, so the title is as follows

Thought analysis

In the last problem, I had a solution function that I didn’t write out, so I found the related linked list problem to do it, to make up for it

If the list is null, use the new node to create a new circular list. Then consider not null, to determine the relationship between the current value and the next pointer value and the inserted value, the maximum and minimum of the inserted value, and the middle position, the middle position needs to find the upper and lower values of the pointer is just greater than or less than it.

var insert = function(head, insertVal) { if(! head){ let list = new Node(insertVal) list.next = list; return list; } let curr = head; while(curr.next! =head){ if((curr.val<=insertVal&&curr.next.val>=insertVal)|| (curr.next.val<curr.val&&(curr. val<=insertVal||insertVal<=curr.next.val))) { break; } curr = curr.next; } curr.next = new Node(insertVal,curr.next); return head; };Copy the code

conclusion

New NodeList() = new NodeList() = new NodeList() = new NodeList()

Persistence is victory. November 5 algorithm completed, persistence is victory!!

Feel free to leave a comment if you have anything to add or clarify.

Practice makes perfect! .

Left left left

→ Algorithm series link ←

Write write write

You can order it here! You can order it here! You can order it here!