0%

leetcode刷题日志01

question 2095

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
class Solution {
public ListNode deleteMiddle(ListNode head) {
int len=getLen(head);
if(len==0) return null;
float half=len/2.0f;
if((len&1)==1){
deleteNode(head,(int)Math.ceil(half));

}else{
deleteNode(head,(int)half);
}
return head;

}
public int getLen(ListNode head) {
ListNode temp=head;
int i=0;
while(temp.next!=null){
temp=temp.next;
i++;
}
return i;
}
public void deleteNode(ListNode head,int n) {
ListNode temp=head;
int i=0;
while(temp.next!=null){
if(i==(n-1))break;
temp=temp.next;
i++;
}
temp.next=temp.next.next;
}
}

question 143

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
class Solution {
public void reorderList(ListNode head) {
ListNode lastptr;
ListNode nextptr;
ListNode firstptr=head;
ListNode targetptr=firstptr;

if(!(head.next==null || head.next.next ==null)){
lastptr=getLast(firstptr.next);
while(targetptr.next!=null){
nextptr=targetptr.next;
targetptr.next=lastptr;
lastptr.next=nextptr;
targetptr=nextptr;
lastptr=getLast(targetptr);
}
if(targetptr !=lastptr)
targetptr.next=lastptr;
}
}
public ListNode getLast(ListNode first) {
ListNode last=first.next;
ListNode temp=first;
while(last !=null && last.next!=null){
if (temp ==null)
break;
last=last.next;
temp=temp.next;
}
temp.next=null;
return last;
}
}