File tree Expand file tree Collapse file tree 3 files changed +167
-0
lines changed Expand file tree Collapse file tree 3 files changed +167
-0
lines changed Original file line number Diff line number Diff line change 1+ package linkedlist .common ;
2+
3+ /**
4+ * Created by LXF on 2016/7/27.
5+ */
6+ public class TNode {
7+ public int value ;
8+ public TNode next ;
9+
10+ public TNode () {
11+ this .next = null ;
12+ }
13+
14+ /*为添加一个节点的方法*/
15+ public void add (int k ) {
16+ TNode t = this ;
17+ while (t .next != null ) {
18+ t = t .next ;
19+ }
20+ TNode node = new TNode ();
21+ node .value = k ;
22+ t .next = node ;
23+ node .next = null ;
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package linkedlist .removeByRatio ;
2+
3+ import linkedlist .common .Node ;
4+ import org .junit .Test ;
5+
6+ /**
7+ * 根据链表长度n,删除链表a/b处的结点,向上取整。
8+ * Created by LXF on 2016/7/27.
9+ */
10+ public class RemoveByRatio {
11+ public Node Remove (Node head , int a , int b ) {
12+ if (a < 1 || a > b ) {
13+ return head ;
14+ }
15+ int n = 0 ;
16+ Node cur = head ;
17+ while (cur != null ) {
18+ n ++;
19+ cur = cur .next ;
20+ }
21+ n = (int ) Math .ceil ((double ) (a * n ) / (double ) b );
22+ if (n == 1 ) {
23+ head = head .next ;
24+ }
25+ if (n > 1 ) {
26+ cur = head ;
27+ while (--n != 1 ) {
28+ cur = cur .next ;
29+ }
30+ cur .next = cur .next .next ;
31+ }
32+ return head ;
33+ }
34+
35+ @ Test
36+ public void test () {
37+ Node head = new Node (2 );
38+ head .add (3 );
39+ head .add (5 );
40+ head .add (8 );
41+ head .add (6 );
42+ RemoveByRatio removeByrRatio = new RemoveByRatio ();
43+ Node res = removeByrRatio .Remove (head , 3 , 4 );
44+ System .out .println (res .next .next .next .value );
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ package linkedlist .reserveList ;
2+
3+ import linkedlist .common .DNode ;
4+ import linkedlist .common .Node ;
5+ import linkedlist .common .TNode ;
6+ import org .junit .Test ;
7+
8+ /**
9+ * Created by LXF on 2016/7/27.
10+ */
11+ public class ReserveList {
12+ /*不带头结点的反转*/
13+ public Node reverse (Node head ) {
14+ Node pre ,next ;
15+ pre = next = null ;
16+ while (head != null ) {
17+ next = head .next ;
18+ head .next = pre ;
19+ pre = head ;
20+ head = next ;
21+ }
22+ return pre ;
23+ }
24+
25+ /*带头结点的头插法反转*/
26+ public TNode reserve1 (TNode head ) {
27+ TNode cur = head .next ;
28+ head .next = null ;
29+ TNode next ;
30+ while (cur != null ) {
31+ next = cur .next ;
32+ cur .next = head .next ;
33+ head .next = cur ;
34+ cur = next ;
35+ }
36+ return head ;
37+ }
38+
39+ /*反转双链表*/
40+ public DNode reserveD (DNode head ) {
41+ DNode pre ,next ;
42+ pre = next = null ;
43+ while (head != null ) {
44+ next = head .next ;
45+ head .next = pre ;
46+ head .last = next ;
47+ pre = head ;
48+ head = next ;
49+ }
50+ return pre ;
51+ }
52+
53+
54+ @ Test
55+ public void test () {
56+
57+ Node head = new Node (2 );
58+ head .add (3 );
59+
60+ ReserveList reserveList = new ReserveList ();
61+ Node res = reserveList .reverse (head );
62+ System .out .println (res .next .value );
63+ }
64+
65+ @ Test
66+ public void testT () {
67+ TNode head = new TNode ();
68+ head .add (3 );
69+ head .add (4 );
70+ head .add (5 );
71+
72+ ReserveList reserveList = new ReserveList ();
73+ TNode res = reserveList .reserve1 (head );
74+ while (res != null ) {
75+ System .out .println (res .value );
76+ res = res .next ;
77+ }
78+ }
79+
80+ @ Test
81+ public void testD () {
82+ DNode head = new DNode (3 );
83+ head .add (4 );
84+ head .add (42 );
85+ head .add (56 );
86+
87+ ReserveList reserveList = new ReserveList ();
88+ DNode res = reserveList .reserveD (head );
89+
90+ while (res != null ) {
91+ System .out .println (res .value );
92+ res = res .next ;
93+ }
94+
95+ }
96+ }
You can’t perform that action at this time.
0 commit comments