Cracking the Coding Interview 第二章
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cracking the Coding Interview 第二章相关的知识,希望对你有一定的参考价值。
2.2 链表中倒数第k个结点
输入一个链表,输出该链表中倒数第k个结点。
思路:快慢指针(error: 判断是否有可行解,否则返回null, while, if 后加空格)
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){ 13 return null; 14 } 15 ListNode fast = head; 16 ListNode ans = head; 17 for (int i = 0; i < k; i++){ 18 if (fast == null){ 19 return null; 20 } 21 fast = fast.next; 22 } 23 while (fast != null){ 24 fast = fast.next; 25 ans = ans.next; 26 } 27 return ans; 28 } 29 }
2.3 删除当前节点
实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。
给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true
思路:将当前的val转为下一个的val,释放下一个即可。
1 import java.util.*; 2 3 /* 4 public class ListNode { 5 int val; 6 ListNode next = null; 7 8 ListNode(int val) { 9 this.val = val; 10 } 11 }*/ 12 public class Remove { 13 public boolean removeNode(ListNode pNode) { 14 // write code here 15 if (pNode == null || pNode.next == null){ 16 return false; 17 } 18 //ListNode next = pNode.next; 19 pNode.val = pNode.next.val; 20 pNode.next = pNode.next.next; 21 return true; 22 } 23 }
2.4 链表分割(小于x的在一边。大于等于x的又在一边)
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
思路:(1)在原链表上找到第一个大的,将之后的小的往前移(error:移完之后不能立即向前,不然会错过连着的两个这种情况)
1 import java.util.*; 2 3 /* 4 public class ListNode { 5 int val; 6 ListNode next = null; 7 8 ListNode(int val) { 9 this.val = val; 10 } 11 }*/ 12 public class Partition { 13 public ListNode partition(ListNode pHead, int x) { 14 // write code here 15 if (pHead == null){ 16 return null; 17 } 18 ListNode front = new ListNode(0); 19 front.next = pHead; 20 pHead = front; 21 while (pHead.next != null && pHead.next.val < x){ 22 pHead = pHead.next; 23 } 24 if (pHead.next != null){ 25 ListNode greater = pHead.next; 26 while (greater.next != null){ 27 if (greater.next.val < x){ 28 ListNode temp = greater.next; 29 greater.next = temp.next; 30 temp.next = pHead.next; 31 pHead.next = temp; 32 pHead = temp; 33 } else { 34 greater = greater.next; 35 } 36 } 37 } 38 pHead = front.next; 39 front = null; 40 return pHead; 41 } 42 }
(2)新建两个链表small large,指向对应元素即可。时间复杂度更大。
1 import java.util.*; 2 3 /* 4 public class ListNode { 5 int val; 6 ListNode next = null; 7 8 ListNode(int val) { 9 this.val = val; 10 } 11 }*/ 12 public class Partition { 13 public ListNode partition(ListNode pHead, int x) { 14 // write code here 15 /*if (pHead == null){ 16 return null; 17 } 18 ListNode front = new ListNode(0); 19 front.next = pHead; 20 pHead = front; 21 while (pHead.next != null && pHead.next.val < x){ 22 pHead = pHead.next; 23 } 24 if (pHead.next != null){ 25 ListNode greater = pHead.next; 26 while (greater.next != null){ 27 if (greater.next.val < x){ 28 ListNode temp = greater.next; 29 greater.next = temp.next; 30 temp.next = pHead.next; 31 pHead.next = temp; 32 pHead = temp; 33 } else { 34 greater = greater.next; 35 } 36 } 37 } 38 pHead = front.next; 39 front = null; 40 return pHead;*/ 41 ListNode small = new ListNode(0); 42 ListNode large = new ListNode(0); 43 ListNode tailSmall = small; 44 ListNode tailLarge = large; 45 while (pHead != null){ 46 if (pHead.val < x){ 47 tailSmall.next = new ListNode(pHead.val); 48 tailSmall = tailSmall.next; 49 } else { 50 tailLarge.next = new ListNode(pHead.val); 51 tailLarge = tailLarge.next; 52 } 53 pHead = pHead.next; 54 } 55 tailSmall.next = large.next; 56 return small.next; 57 } 58 }
以上是关于Cracking the Coding Interview 第二章的主要内容,如果未能解决你的问题,请参考以下文章
Cracking the Coding Interview 第二章
[Cracking the Coding Interview] 4.3 List of Depths
[Cracking the Coding Interview] 4.2 Minimal Tree 最小树
[Cracking the Coding Interview] 4.5 Validate BST
Cracking the Coding Interview, Binary Tree, Binary Search Tress