-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
162 lines (151 loc) · 4.05 KB
/
Copy pathLinkedList.java
File metadata and controls
162 lines (151 loc) · 4.05 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
public class LinkedList {
public ListNode middleNode(ListNode head) {
ListNode temp = head, tempc = head;
while (tempc != null && tempc.next != null) {
temp = temp.next;
tempc = tempc.next.next;
}
return temp;
}
public ListNode reverseList(ListNode head) {//for the diagram visit https://youtu.be/G0_I-ZF0S38?t=118
ListNode curr = head, prev = null, temp;
while (curr != null) {
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
public static ListNode deleteDuplicates(ListNode head) {
ListNode curr = head;
while (curr != null && curr.next != null) {
if (curr.val == curr.next.val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
return head;
}
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode out = new ListNode(0), dum = out, p1 = list1, p2 = list2;
while (p1 != null && p2 != null) {
if (p1.val < p2.val) {
dum.next = p1;
p1 = p1.next;
} else {
dum.next = p2;
p2 = p2.next;
}
dum = dum.next;
}
if (p1 != null) {
dum.next = p1;
}
if (p2 != null) {
dum.next = p2;
}
return out.next;
}
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode s = head, f = head;
while (f != null && f.next != null) {
s = s.next;
f = f.next.next;
if (f==s) {
return true;
}
}
return false;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head=null;
ListNode curr=null;
int a=0,b=0;
while (l1!=null) {
a=a*10+l1.val;
l1=l1.next;
}
while (l2!=null) {
b=b*10+l2.val;
l2=l2.next;
}
int c=a+b;
if (c==0) {
return new ListNode(0);
}
while (c!=0) {
int digit=c%10;
ListNode newN = new ListNode(digit);
if (head==null) {
head=newN;
curr=head;
} else {
curr.next=newN;
curr=curr.next;
}
System.out.println(digit);
c/=10;
}
return head;
}
public static String to_String(ListNode head) {
StringBuilder sb = new StringBuilder();
ListNode current = head;
while (current != null) {
sb.append(current.val);
if (current.next != null) {
sb.append(" -> ");
}
current = current.next;
}
return sb.toString();
}
public static ListNode removeNthFromEnd(ListNode head, int n) {
int len=0;
ListNode temp=head;
while(temp!=null) {
len++;
temp=temp.next;
}
if (len==1) {
return null;
}
if (len==2&&n==2) {
head=head.next;
return head;
}
temp=head;
for (int i = 0; i < len-n-1; i++) {
temp=temp.next;
}
temp.next=temp.next.next;
temp=head;
while(temp!=null) {
System.out.print(temp.val+"->");
temp=temp.next;
}
return temp;
}
public static void main(String[] args) {
ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, null)))));
System.out.println(removeNthFromEnd(head,2));
}
}