A question: [the list penultimate k nodes]
Input a linked list, the linked list output reciprocal k-th node.
Analysis: pointer speed, faster than the slow pointer pointers go step k-1, come to the end of the pointer when the fast, slow town position of the pointer is the penultimate node k;
1 /*
2 public class ListNode {
3 int val;
4 ListNode next = null;
5
6 ListNode(int val) {
7 this.val = val;
8 }
9 }*/
10 public class Solution {
11 public ListNode FindKthToTail(ListNode head,int k) {
12 if(head==null||k<=0) return null;
13 ListNode fNode = head;
14 for(int i=1;i<k;i++){
15 fNode = fNode.next;
16 if(fNode==null) return null;
17 }
18 while(fNode.next!=null){
19 head = head.next;
20 fNode = fNode.next;
21 }
22 return head;
23 }
24 }
Question two: [list] reverse
After entering a list inverted list, the new list of the output header.
Analysis: setting pre, cur, next three pointers, pointing to a previous node of the current node, the current node, the current node is a node;
1 /*
2 public class ListNode {
3 int val;
4 ListNode next = null;
5
6 ListNode(int val) {
7 this.val = val;
8 }
9 }*/
10 public class Solution {
11 public ListNode ReverseList(ListNode head) {
12 ListNode pre=null;
13 ListNode next=null;
14 ListNode cur = head;
15 while(cur!=null){
16 next=cur.next;
17 cur.next=pre;
18 pre=cur;
19 cur=next;
20 }
21 return pre;
22 }
23 }
Question three: [merge two sorted lists]
Two monotonically increasing input list and output list after synthesis of two lists, of course, after that we need to meet synthesis list - decreasing the rules.
Analysis: The second linked list is inserted into a first linked list, if the value of the second list list2 node is greater than a value of the first node in the list list1 and less than a first linked list of the next node list1.next value, then inserted into list1 and list2 in list1.next.
1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode Merge(ListNode list1,ListNode list2) { 12 if(list1==null) return list2; 13 if(list2==null) return list1; 14 ListNode head = list1; 15 while(list1!=null&&list2!=null){ 16 if(list1.val<=list2.val&&(list1.next==null||list1.next.val>list2.val)){ 17 ListNode next2 = list2.next;//画个图就清晰了 18 list2.next = list1.next; 19 list1.next = list2; 20 list2 = next2; 21 list1 = list1.next; 22 }else{ 23 list1 = list1.next; 24 } 25 } 26 return head; 27 } 28 }
法二:递归
1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode Merge(ListNode list1,ListNode list2) { 12 if(list1==null) return list2; 13 if(list2==null) return list1; 14 if(list1.val<=list2.val){ 15 list1.next = Merge(list1.next,list2); 16 return list1; 17 }else{ 18 list2.next = Merge(list1,list2.next); 19 return list2; 20 } 21 } 22 }
题四:【树的子结构】
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
分析:使用递归,先查找B树在A中的位置(递归方法一),然后递归查找A.left,B.left和A.right,B.right(递归方法二);
1 /** 2 public class TreeNode { 3 int val = 0; 4 TreeNode left = null; 5 TreeNode right = null; 6 7 public TreeNode(int val) { 8 this.val = val; 9 10 } 11 12 } 13 */ 14 public class Solution { 15 public boolean HasSubtree(TreeNode root1,TreeNode root2) { 16 if(root1==null||root2==null) return false; 17 return isSubtree(root1,root2)//root1.val=root2.val,下面两个是不等情况下进行 18 ||HasSubtree(root1.left,root2)//从root1左子树找 19 ||HasSubtree(root1.right,root2);//从root1右子树找 20 } 21 22 public boolean isSubtree(TreeNode root1, TreeNode root2){ 23 if(root2==null) return true; 24 if(root1==null)return false;//root1==null&&root2!=null 25 if(root1.val==root2.val){ 26 return isSubtree(root1.left,root2.left)&&isSubtree(root1.right,root2.right); 27 }else{ 28 return false; 29 } 30 } 31 }