/
LinkedListExecise2.java
131 lines (118 loc) · 3.19 KB
/
LinkedListExecise2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.kyson.chapter1.section3;
/***
* 1.3.20 编写一个方法delete(),接受一个int参数k,删除链表的第k个元素(如果它存在的话)。
*
* 1.3.20 Write a method delete() that takes an int argument k and deletes the
* kth element in a linked list, if it exists.
*
*/
public class LinkedListExecise2<Item> {
private static class Node<Item> {
Node next;
Item item;
}
/**
*
* @param k 第K个元素
* @param first 链表的首节点
* @return 新的链表
* @throws Exception
*/
// public Node<Item> delete(int k, Node<Item> first) throws Exception {
// if (k <= 0 || first == null) return null;
//
// Node<Item> current1 = first;
// int count = 0;
// //先计算链表总长度
// while (current1.next != null) {
// current1 = current1.next;
// count++;
// }
// //如果K的大小大于总长度 ,抛出异常
// if (k >= count) {
// throw new Exception();
// }
//
// Node<Item> current2 = first;
// if (k == 0) {
// first = first.next;
// return first;
// }
//
// int index = 0;
// while (current2.next != null) {
// if (index == k - 1) {
// current2.next = current2.next.next;
// }
// current2 = current2.next;
// index++;
// }
// return first;
// }
public Node<Item> delete(int k, Node<Item> first) throws Exception{
if (k < 0 || first == null) return null;
if (k == 0) {
first = first.next;
return first;
}
Node<Item> current = first;
while (current != null && --k != 0) {
current = current.next;
}
if (k!= 0 || current.next == null || current == null) {
throw new Exception();
}else {
current.next = current.next.next;
}
return first;
}
public static void main(String[] args) {
/**
* 创建链表
* */
Node<String> first = new Node<String>();
Node<String> second = new Node<String>();
Node<String> third = new Node<String>();
Node<String> forth = new Node<String>();
Node<String> fifth = new Node<String>();
first.item = "我的";
first.next = second;
second.item = "名字";
second.next = third;
third.item = "叫";
third.next = forth;
forth.item = "顶级程序员不穿女装";
forth.next = fifth;
fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
fifth.next = null;
// 删除尾节点之前
System.out.println("原链表:\n-------");
Node current1 = first;
while (current1.next != null) {
System.out.println(current1.item);
current1 = current1.next;
}
System.out.println(current1.item);
System.out.println("-------");
LinkedListExecise2<String> linkedListExercise2 = new LinkedListExecise2<String>();
// 删除第一个元素
int k = 4;
System.out.println("正在删除链表第" + k + "个节点...");
Node<String> resultNode = null;
try {
resultNode = linkedListExercise2.delete(k,first);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除成功");
System.out.println("新链表:\n-------");
Node current2 = resultNode;
while (current2.next != null) {
System.out.println(current2.item);
current2 = current2.next;
}
System.out.println(current2.item);
System.out.println("-------");
}
}