You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.8 KiB
1.8 KiB
链表相关
class Solution {
public int[] reversePrint(ListNode head) {
ListNode temp = head;
int size = 0;
while (temp != null) {
temp = temp.next;
size++;
}
int[] result = new int[size];
int index = size - 1;
while (head != null) {
result[index--] = head.val;
head = head.next;
}
return result;
}
}
class Solution {
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack = new Stack<ListNode>();
ListNode temp = head;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
int size = stack.size();
int[] print = new int[size];
for (int i = 0; i < size; i++) {
print[i] = stack.pop().val;
}
return print;
}
}
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
int length = 0;
ListNode temp = head;
while (temp != null) {
temp = temp.next;
length++;
}
for (int i = 0; i < length - k; i++) {
head = head.next;
}
return head;
}
}
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode h0 = head;
for (int i = 0; i < k; i++) {
h0 = h0.next;
}
while (h0 != null) {
h0 = h0.next;
head = head.next;
}
return head;
}
}