JavaScript fun: Merge and sort linked lists

Source: Internet
Author: User
As we all know, the basic idea of Merge Sorting is a process of first splitting and then merging. Then, how can we sort a single-chain table by merging? As we all know, the basic idea of Merge Sorting is a process of first splitting and then merging.

Then, how can we sort a single-chain table by merging?

First, we need a method to split the linked list, as shown in the following pseudo code:

var source = 1 -> 3 -> 7 -> 8 -> 11 -> 12 -> 14 -> null  var front = new Node()  var back = new Node()  frontBackSplit(source, front, back)  front === 1 -> 3 -> 7 -> 8 -> null  back === 11 -> 12 -> 14 -> null

It receives the tail pointer of a linked list as a parameter and splits it into two parts, that is, the first half and the second half.

How can we determine the intermediate demarcation value?

You can use the speed pointer, the speed pointer and the slow pointer to start from the tail at the same time and traverse the single-chain table. The fast pointer takes two steps at a time, and the slow pointer takes one step at a time, then the fast pointer will surely reach the end point first, and the slow pointer is only half the distance at this time. That is to say, the slow pointer is at this demarcation point.

The rest is easy to do. Cut off at the boundary and set it to null. The first stage is complete.

function Node(data) {      this.data = data === undefined ? null : data;      this.next = null;  }    function frontBackSplit(source, front, back) {      var total = 0;      var fast = source;      var slow = source;      var partial = null;      while(fast && fast.next){          fast = fast.next.next;          slow = slow.next;          total++;      }      partial = slow;      while(slow){          slow = slow.next;          total++;      }      if(total % 2 === 1){          back.data = partial.next.data;          back.next = partial.next.next;          partial.next = null;      }      else{          back.data = partial.data;          back.next = partial.next;          for(var e=source;e.next!=partial;e=e.next);          e.next = null;            }      front.data = source.data;      front.next = source.next;  }

Then, the linked list is broken down and even becomes an inseparable unit node. We need to find a way to combine them and assemble them into a new ordered linked list. Therefore, we need the following merge method:

var first = 2 -> 4 -> 6 -> 7 -> null  var second = 1 -> 3 -> 5 -> 6 -> 8 -> null  sortedMerge(first, second) === 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 6 -> 7 -> 8 -> null

To write such a method, there are actually a lot of cases to consider, which is reflected in the Code:

function sortedMerge(l1, l2) {      var newList = null;      var temp = null;      while(l1 && l2){          if(l1.data > l2.data){              if(!newList){                  newList = l2;                  temp = l2;              }              else{                  temp.next = l2;                  temp = l2;              }              l2 = l2.next;          }          else{              if(!newList){                  newList = l1;                  temp = l1;              }              else{                  temp.next = l1;                  temp = l1;              }              l1 = l1.next;          }      }      if(l1){          if(!newList){              newList = l1;          }          else{              temp.next = l1;          }      }      if(l2){          if(!newList){              newList = l2;          }          else{              temp.next = l2;          }      }      return newList;  }

Well, the splitting and merging methods are all written, just like the chopping board and kitchen knife are ready, and you only need to cut the meat. The main method is a recursive process.

function mergeSort(list) {      if(list && list.next){          var front = new Node();          var back = new Node();          frontBackSplit(list, front, back);          return sortedMerge(mergeSort(front),mergeSort(back));      }      return list;  }

The above is the JavaScript interesting question: the content of the merged sorting of the linked list. For more information, see PHP Chinese Network (www.php1.cn )!

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.