leet链表
ldd儆儆
这个作者很懒,什么都没留下…
展开
-
leet23:合并K个排序链表
public ListNode mergeKLists(ListNode[] lists) { int len = lists.length; if (len == 0) { return null;} // 将n个链表以中间为对称,合并,即合并 while(len>1) { for (int i=0; i<len/2; i++) { lists[i...原创 2019-12-31 16:09:23 · 114 阅读 · 0 评论 -
leet86:分隔链表
public ListNode partition(ListNode head, int x) {//1ms 63% ListNode lessHead = new ListNode(0); ListNode moreHead = new ListNode(0); ListNode lessPre = lessHead; ListN...原创 2019-12-31 11:20:51 · 218 阅读 · 0 评论 -
leet92:反转链表 II
public ListNode reverseBetween(ListNode head, int m, int n) { ListNode res = new ListNode(0); res.next = head; ListNode node = res; //找到需要反转的那一段的上一个节点。 for (int i = 1; i < m; i++) { no...原创 2019-12-31 10:06:53 · 103 阅读 · 0 评论 -
leet142环形链表||
public ListNode detectCycle(ListNode head) { Set<ListNode> set = new HashSet<>(); while(head!=null){ if(set.contains(head)) return head; ...原创 2019-12-16 11:45:54 · 87 阅读 · 0 评论 -
leet61旋转链表
public ListNode rotateRight(ListNode head, int k) { // base cases if (head == null) return null; if (head.next == null) return head; // close the linked list into the ring ListN...原创 2019-12-16 11:43:33 · 59 阅读 · 0 评论 -
leet148:排序链表
public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; ListNode fast = head.next, slow = head; while (fast != null && fast.next != ...原创 2019-12-16 11:50:28 · 114 阅读 · 0 评论 -
leet328:奇偶链表
给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。示例 1:输入: 1->2->3->4->5->NULL输出: 1->3->5->2-&g...原创 2019-12-16 11:50:38 · 304 阅读 · 0 评论 -
leet83: 删除排序链表中的重复元素
public ListNode deleteDuplicates(ListNode head) { ListNode curr = head; while(curr!=null &&curr.next!=null ){ if(curr.val == curr.next.val) curr....原创 2019-12-16 13:44:47 · 66 阅读 · 0 评论 -
leet160:相交链表
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode ha = headA, hb = headB; while (ha != hb) { ha = ha != null ? ha.next : headB; hb = hb != null ...原创 2019-12-16 11:47:52 · 145 阅读 · 0 评论 -
leet203:移除指定的所有链表元素
public ListNode removeElements(ListNode head, int val) { //删除值相同的头结点后,可能新的头结点也值相等,用循环解决 while(head!=null&&head.val==val){ head=head.next; } if(hea...原创 2019-12-16 11:48:12 · 150 阅读 · 0 评论 -
leet876:找出中间链表
时间复杂度:O(N)O(N)O(N),其中 NNN 是给定列表中的结点数目。空间复杂度:O(N)O(N)O(N),A 用去的空间。 public ListNode middleNode(ListNode head) { ListNode[] A = new ListNode[100]; int t = 0; while (head.next !...原创 2019-12-16 11:49:24 · 78 阅读 · 0 评论 -
leet206:反转链表
使用递归public ListNode reverseList(ListNode head) { if (head == null || head.next == null) return head; ListNode p = reverseList(head.next); head.next.next = head; head.next = null...原创 2019-12-16 11:49:02 · 89 阅读 · 0 评论 -
leet141:判断链表是否有环
使用双指针运行0ms两种方法时间复杂度均为O(n) 空间复杂度为O(1)if (head == null || head.next == null) { return false;}ListNode slow = head;ListNode fast = head.next;while (slow != fast) { if (fast == null || fast....原创 2019-12-16 11:47:36 · 66 阅读 · 0 评论 -
leet876:链表中间点
输出到数组 public ListNode middleNode(ListNode head) { ListNode[] A = new ListNode[100]; int t = 0; while (head.next != null) { A[t++] = head; head = head....原创 2019-12-16 11:48:52 · 83 阅读 · 0 评论 -
leet237:删除节点
原创 2019-12-16 11:49:47 · 106 阅读 · 0 评论 -
leet21:合并有序链表
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } else if (l2 == null) { return l1; } else if (l1.val < l2.val) { ...原创 2019-12-16 11:48:39 · 69 阅读 · 0 评论