//206.反转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
ListNode temp = null;
while(cur != null){
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
//24. 两两交换链表中的节点
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummyhead = new ListNode(-1,head);
ListNode cur = dummyhead;
ListNode first;
ListNode second;
ListNode temp;
while(cur.next != null && cur.next.next != null){
temp = cur.next.next.next;
first = cur.next;
second = cur.next.next;
cur.next = second;
cur.next.next = first;
first.next = temp;
cur = first;
}
return dummyhead.next;
}
}
//19.删除链表的倒数第N个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummynode= new ListNode(-1,head);
ListNode slowIndex = dummynode;
ListNode fastIndex = dummynode;
for(int i=0 ; i<=n ; i++){
fastIndex = fastIndex.next;
}
while(fastIndex != null){
slowIndex = slowIndex.next;
fastIndex = fastIndex.next;
}
slowIndex.next = slowIndex.next.next;
return dummynode.next;
}
}