#### 链表的反转

solution1

``````//time complexity:O(n),space complexity:O(1)
ListNode pre = null, curr = head, next = null;
while(curr!=null){
next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
``````

solution2

``````//time complexity:O(n),space complexity:O(n)
return newNode;
}
``````

#### 快慢指针:

###### 1.给定一个链表，如何找到链表的中间点？

``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/

while(fast.next!=null&&fast.next.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}

//OR

while(fast!=null&&fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
``````

``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/
public class Solution {
while(fast!=null&&fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast){
return true;
}
}
return false;
}
}
``````
###### 3.Insert a value in a sorted linked list.给定一个sorted好的链表和一个value值,将value值插入到链表正确的位置。
``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/
public class Solution {
//tiem complexity:O(n); space complexity:O(1)
public ListNode insert(ListNode head, int value) {
//corner case
ListNode node = new ListNode(value);
return node;
}

ListNode prev = null;
while(curr!=null){
if(curr.value < value){
prev = curr;
curr = curr.next;
}else{
break;
}
}
prev.next = node;
node.next = curr;

}
}
``````
###### 4.Merge two sorted lists into one large sorted list.合并两个排序好的链表
``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/
public class Solution {
//tiem complexity:O(n); space complexity:O(1)
public ListNode merge(ListNode one, ListNode two) {
if(one == null)return two;
if(two == null)return one;
ListNode curr1 = one;
ListNode curr2 = two;
while(curr1!=null&&curr2!=null){
if(curr1.value<=curr2.value){
curr.next = curr1;
curr1=curr1.next;
}else{
curr.next = curr2;
curr2 = curr2.next;
}
curr = curr.next;
}
if(curr1!=null){
curr.next = curr1;
}
if(curr2!=null){
curr.next = curr2;
}
}
}
``````
###### 5.Linkedlist Partition给定一个链表和一个目标值t，将其划分为所有小于T的节点在大于或等于目标值T的节点之前列出，应该保留两个分区中每个节点的原始相对顺序。L = 2 -> 4 -> 3 -> 5 -> 1 -> null, T = 3, is partitioned to 2 -> 1 -> 4 -> 3 -> 5 -> null
``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/
public class Solution {
//tiem complexity:O(n); space complexity:O(1)
public ListNode partition(ListNode head, int target) {
ListNode dummyNode1 = new ListNode(0);
ListNode dummyNode2 = new ListNode(0);
ListNode curr1 = dummyNode1;
ListNode curr2 = dummyNode2;
while(curr!=null){
if(curr.value<target){
curr1.next=curr;
curr1 =curr1.next;
}else{
curr2.next = curr;
curr2 = curr2.next;
}
curr = curr.next;
}
curr1.next = dummyNode2.next;
curr2.next = null;
return dummyNode1.next;
}
}

``````
###### 6.给定一个单链表L: L0→L1→…→Ln-1→Ln,重新排列后为：L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点值的情况下进行原地操作。

``````/**
* class ListNode {
*   public int value;
*   public ListNode next;
*   public ListNode(int value) {
*     this.value = value;
*     next = null;
*   }
* }
*/
public class Solution {

//tiem complexity:O(n); space complexity:O(1)

ListNode tail = reverse(mid.next);
mid.next = null;

}

//tiem complexity:O(n); space complexity:O(1)
ListNode dummy = new ListNode(0);
int index = 0;
if(index%2==0){
}else{
}
index++;
dummy = dummy.next;
}
}else{
}
}

//tiem complexity:O(n); space complexity:O(1)
ListNode pre = null, curr = head, next = null;
while(curr!=null){
next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}

//tiem complexity:O(n); space complexity:O(1)
while(fast!=null&&fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}

}

``````