链表中倒数第k个结点
题目描述
输入一个链表,输出该链表中倒数第k个结点。
解法一:ArrayList
- 用一个ArrayList 存储遍历的Node节点
- 然后可以直接返回你想要的节点了。。
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k <= 0){
return null;
}
ArrayList<ListNode> res = new ArrayList();
ListNode cur = head;
while(cur != null){
res.add(cur);
cur = cur.next;
}
if(k > res.size()){
return null;
}
return res.get(res.size() - k);
}
}
解法二:双指针
- 双指针 让一个指针先走k-1 步,然后再让新的指针从头开始走,等到老的指针走到头,新的指针位置就是倒数第K个节点
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k <= 0){
return null;
}
ListNode fast = head;
ListNode slow = head;
int count = 0;
for(int i = 0;i < k - 1;i++){
if(fast.next != null){
fast = fast.next;
}else{
return null;
}
}
while(fast.next != null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
反转链表
题目描述
输入一个链表,反转链表后,输出新链表的表头。
解法一:迭代。三个指针
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null){
return null;
}
if(head != null && head.next != null && head.next.next == null){
ListNode tem = head.next;
head.next = null;
tem.next = head;
return tem;
}
if(head!=null && head.next == null){
return head;
}
ListNode pNode = null;
ListNode cur = head;
ListNode pre = null;
while(cur != null){
ListNode next = cur.next;
if(next == null){
pNode = cur;
}
cur.next = pre;
pre = cur;
cur = next;
}
return pNode;
}
}
合并两个排序的链表
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
解法一:迭代
- 感觉是最笨的方法了。。
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
ArrayList<ListNode> c = new ArrayList();
if(list1 == null && list2 == null){
return null;
}
if(list1 == null && list2 != null){
return list2;
}
if(list1 != null && list2 == null){
return list1;
}
ListNode l1 = list1;
ListNode l2 = list2;
ListNode p = null;
ListNode root = null;
while(l1 != null && l2 != null){
if(l1.val >= l2.val){
if(p == null){
p = l2;
root = p;
}else{
p.next = l2;
p = p.next;
}
l2 = l2.next;
}else{
if(p == null){
p = l1;
root = p;
}else{
p.next = l1;
p = p.next;
}
l1 = l1.next;
}
}
while(l1 != null){
p.next = l1;
p = p.next;
l1 = l1.next;
}
while(l2 != null){
p.next = l2;
p = p.next;
l2 = l2.next;
}
return root;
}
}
解法二:递归
- 递归方法
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1 == null && list2 == null){
return null;
}
if(list1 == null && list2 != null){
return list2;
}
if(list1 != null && list2 == null){
return list1;
}
return get(list1, list2);
}
private ListNode get(ListNode list1, ListNode list2){
ListNode l1 = list1;
ListNode l2 = list2;
if(l1 == null){
return l2;
}else if(l2 == null){
return l1;
}
ListNode root = null;
if(l1.val >= l2.val){
root = l2;
root.next = get(l1, l2.next);
}else{
root = l1;
root.next = get(l1.next, l2);
}
return root;
}
}
树的子结构
解法一:递归判断
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
boolean res = false;
if(root1 != null && root2 != null){
if(root1.val == root2.val){
res = isHas(root1, root2);
}
if(!res){
res = HasSubtree(root1.left, root2);
}
if(!res){
res = HasSubtree(root1.right, root2);
}
}
return res;
}
public boolean isHas(TreeNode t1, TreeNode t2){
if(t2 == null){
return true;
}
if(t1 == null){
return false;
}
if(t1.val != t2.val){
return false;
}
return isHas(t1.left, t2.left) && isHas(t1.right, t2.right);
}
}