2.2 Kth element linked list

Problem

Implement an algorithm to find the kth to last element of a singly linked list.

Solution

 1 public static ListNode findElement(ListNode head, int k) {
 2     if(k <= 0)    return null;
 3
 4     ListNode fast = head;
 5     ListNode slow = head;
 6
 7     for(int i=0; i<k-1; i++) {
 8         if(fast == null) return null; //edge case
 9         fast = fast.next;
10     }
11
12     //fast = null
13     if(fast == null) {
14         return null;
15     }
16
17     while(fast.next != null) {
18         fast = fast.next;
19         slow = slow.next;
20     }
21
22     return slow;
23 }
时间: 2024-11-21 02:42:21

2.2 Kth element linked list的相关文章

Kth element of Two Sorted Arrays

1 public class FindKthSorted { 2 //Method 1: time complexity O(k), no extra space 3 4 public int findKth (int[] a, int[] b, int k) { 5 int lenA = a.length; 6 int lenB = b.length; 7 if (k > lenA + lenB) { 8 throw new RuntimeException("Cannot find&q

[email&#160;protected] find kth smallest element in two sorted arrays (O(log n time)

The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra space of O(m+n). The linear run time is pretty good, but could we improve it even further? A better way, O(k): There is

Leetcode: K-th Smallest in Lexicographical Order

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. Note: 1 ≤ k ≤ n ≤ 109. Example: Input: n: 13 k: 2 Output: 10 Explanation: The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so

算法(Algorithms)第4版 练习 1.3.20

方法实现: //1.3.20 /** * delete the kth element in a linked list, if it exists. * * @param k the kth element, it should larger than 1 * @throws IllegalArgumentException if k < 1 * @throws NoSuchElementException if the size of the list is less than k */ p

算法(Algorithms)第4版 练习 链表类 1.3.19~1.3.29

package com.qiusongde.linkedlist; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedList<Item> implements Iterable<Item> { private Node<Item> first; //Node should be public static in this class //When

程序猿的自我修养清单

Data Structures 1. Integer – find number of 1s – next largest smaller – smallest larger number – determine if is palindrom – itoa, atoi – add 2 numbers w/o using + or arithmetic operators – implement *, -, / using only + – find max of two numbers w/o

1.3.20

question: Write a method delete() that takes an int argument k and deletes the kth element in a linked list, if it exists. answer: import edu.princeton.cs.algs4.*; public class Linklist<Item> { private static class Node<Item> { Item item; Node

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a

LeetCode——Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.