List综合应用
来源:互联网 发布:微信淘客软件 百度云 编辑:程序博客网 时间:2024/06/08 11:56
148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *///第一种方法,mergeSort//①找中点//②把两边排好序//③merge两个有序数组// class Solution {// //寻找中点,通过快慢指针实现// public ListNode findMiddle(ListNode head){// ListNode fast = head.next,slow = head;// while(fast!=null && fast.next!=null){// slow = slow.next;// fast = fast.next.next;// }// return slow;// } // public ListNode merge(ListNode left,ListNode right){// ListNode dummpy = new ListNode(0);// ListNode lastNode = dummpy;// while(left != null && right != null){// if(left.val < right.val){// lastNode.next = left;// left = left.next;// }else{// lastNode.next = right;// right = right.next;// }// lastNode = lastNode.next;// }// if(left != null)// lastNode.next = left;// if(right != null)// lastNode.next = right;// return dummpy.next;// }// public ListNode sortList(ListNode head) {// if(head == null || head.next == null) return head;//如果不加head.next==null会陷入死循环// ListNode middle = findMiddle(head);// ListNode left = sortList(middle.next);// middle.next = null;// ListNode right = sortList(head);// return merge(left,right);// }// }//第二种方法,quickSort//①找中点//②根据中点partition List//③对两个链表分别排序//④连接链表class Solution { //寻找中点,通过快慢指针实现 public ListNode findMiddle(ListNode head){ ListNode fast = head.next,slow = head; while(fast!=null && fast.next!=null){ slow = slow.next; fast = fast.next.next; } return slow; } public ListNode getTail(ListNode head){ if(head == null) return head; while(head.next!= null) head = head.next; return head; } public ListNode concat(ListNode left,ListNode middle,ListNode right){ ListNode dummpy = new ListNode(0), tail = dummpy; tail.next = left;tail = getTail(tail); tail.next = middle;tail = getTail(tail); tail.next = right; return dummpy.next; } public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head;//如果不加head.next==null会陷入死循环 ListNode middle = findMiddle(head); //partition链表 ListNode leftDummpy = new ListNode(0),leftTail = leftDummpy; ListNode rightDummpy = new ListNode(0),rightTail = rightDummpy; ListNode middleDummy = new ListNode(0), middleTail = middleDummy; while(head != null){ if(head.val < middle.val){ leftTail.next = head; leftTail = head; }else if(head.val > middle.val){ rightTail.next = head; rightTail = head; }else{ middleTail.next = head; middleTail = head; } head = head.next; } leftTail.next = null; middleTail.next = null; rightTail.next = null; ListNode left = sortList(leftDummpy.next); ListNode right = sortList(rightDummpy.next); return concat(left,middleDummy.next,right); }}
143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes' values.
For example,
Given {1,2,3,4}
, reorder it to {1,4,2,3}
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *///寻找中点//根据中点把链表分成两半,并且逆转右边//merge两个左右链表class Solution { public void reorderList(ListNode head) { if(head == null || head.next == null) return ; ListNode middle = findMiddle(head); ListNode tail = reverse(middle.next); middle.next = null; merge(head,tail); } public ListNode findMiddle(ListNode head){ ListNode slow = head,fast = head.next; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; } return slow; } public ListNode reverse(ListNode head){ ListNode pre = null; while(head != null){ ListNode temp = head.next; head.next = pre; pre = head; head = temp; } return pre; } public void merge(ListNode head,ListNode tail){ ListNode dummpy = new ListNode(0); int index = 0; while(head != null && tail != null){ if(index % 2 == 0){ dummpy.next = head; head = head.next; }else{ dummpy.next = tail; tail = tail.next; } index++; dummpy = dummpy.next; } if(head != null) dummpy.next = head; else dummpy.next = tail; }}
阅读全文
0 0
- List综合应用
- list、set、map集合的综合应用
- 集合List与面向对象编程综合应用
- foreach()与list()的综合应用,用list给嵌套的数组解包
- SQL综合应用学习
- J2EE综合应用
- J2EE综合应用
- MSHFlexGrid的综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- JFreeChart综合应用
- SQL综合应用学习
- T-SQL综合应用
- 信息技术综合应用系统
- 综合应用友情链接管理
- oracle函数综合应用
- Swift开发之3DTouch实用演练
- linux tail 命令详解
- Hadoop与Spark算法分析(一)——WordCount
- 机器学习网址
- [BZOJ 1925][SDOI 2010] 地精部落 DP/递推
- List综合应用
- 2048-AI程序算法分析
- 程序员,如何从平庸走向理想?
- 浅谈java中的祖先类Object
- Fiddler抓包6-get请求(url详解)
- 截取字符串,改变第N个字符的颜色;自定义方法,oncreat中调用
- Latex: 排版基础
- shiro讲解之 Authentication
- Linux命令小结