首页 > 代码库 > Cracking the Coding Interview 第二章

Cracking the Coding Interview 第二章

2.2 链表中倒数第k个结点

技术分享
输入一个链表,输出该链表中倒数第k个结点。
View Code

思路:快慢指针(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 }
View Code

2.3 删除当前节点

技术分享
实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。
给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true
View Code

思路:将当前的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 }
View Code

2.4 链表分割(小于x的在一边。大于等于x的又在一边)

技术分享
编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前
给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针。注意:分割以后保持原来的数据顺序不变。
View Code

思路:(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 }
View Code

(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 }
View Code

 

Cracking the Coding Interview 第二章