1、移除元素(力扣27)
public static int removeElement ( int [ ] nums, int val) {
int left = 0 , right = nums. length - 1 ;
while ( left <= right) {
if ( nums[ left] == val) {
nums[ left] = nums[ right] ;
right -- ;
} else {
left ++ ;
}
}
return left;
}
public static int removeElement ( int [ ] nums, int val) {
int slowIndex = 0 ;
int fastIndex;
for ( fastIndex = 0 ; fastIndex < nums. length; fastIndex ++ ) {
if ( nums[ fastIndex] != val) {
nums[ slowIndex] = nums[ fastIndex] ;
slowIndex ++ ;
}
}
return slowIndex;
}
2、删除有序数组中的重复项(力扣26)
public int removeDuplicates ( int [ ] nums) {
int slow = 0 , fast = 0 ;
while ( fast < nums. length) {
if ( nums[ slow] != nums[ fast] ) {
slow ++ ;
nums[ slow] = nums[ fast] ;
}
fast ++ ;
}
return slow + 1 ;
}
3、移动零(力扣283)
public void moveZeroes ( int [ ] nums) {
int slow = 0 , fast = 0 ;
while ( fast < nums. length) {
if ( nums[ fast] != 0 ) {
nums[ slow] = nums[ fast] ;
slow ++ ;
}
fast ++ ;
}
while ( slow < nums. length) {
nums[ slow ++ ] = 0 ;
}
}
4、比较含退格的字符串(力扣844)
public static boolean backspaceCompare ( String s, String t) {
int i = s. length ( ) - 1 , j = t. length ( ) - 1 ;
int countS = 0 , countT = 0 ;
while ( i >= 0 || j >= 0 ) {
while ( i >= 0 ) {
if ( s. charAt ( i) == '#' ) {
countS++ ;
i-- ;
} else if ( countS > 0 ) {
countS-- ;
i-- ;
} else {
break ;
}
}
while ( j >= 0 ) {
if ( t. charAt ( j) == '#' ) {
countT++ ;
j-- ;
} else if ( countT > 0 ) {
countT-- ;
j-- ;
} else {
break ;
}
}
if ( i >= 0 && j >= 0 ) {
if ( s. charAt ( i) != t. charAt ( j) ) {
return false ;
}
} else {
if ( i >= 0 || j >= 0 ) {
return false ;
}
}
i-- ;
j-- ;
}
return true ;
}
5、有序数组的平方(力扣977)
public int [ ] sortedSquares ( int [ ] nums) {
int len = nums. length;
int [ ] res = new int [ len] ;
int index = len - 1 ;
int left = 0 , right = len - 1 ;
while ( left <= right) {
if ( nums[ left] * nums[ left] >= nums[ right] * nums[ right] ) {
res[ index -- ] = nums[ left] * nums[ left] ;
left ++ ;
} else {
res[ index -- ] = nums[ right] * nums[ right] ;
right -- ;
}
}
return res;
}
6、反转字符串(力扣344)
public void reverseString ( char [ ] s) {
int left = 0 , right = s. length - 1 ;
while ( left < right) {
char temp = s[ left] ;
s[ left] = s[ right] ;
s[ right] = temp;
left ++ ;
right -- ;
}
}
7、反转链表(力扣206)
public ListNode reverseList ( ListNode head) {
ListNode preNode = null ;
ListNode curNode = head;
while ( curNode != null ) {
ListNode nextNode = curNode. next;
curNode. next = preNode;
preNode = curNode;
curNode = nextNode;
}
return preNode;
}
8、 删除链表的倒数第 N 个结点(力扣19)
public static ListNode removeNthFromEnd ( ListNode head, int n) {
ListNode node = new ListNode ( 0 ) ;
node. next = head;
ListNode first = node;
ListNode second = node;
int count = 0 ;
while ( first. next != null ) {
if ( count >= n) {
second = second. next;
}
first = first. next;
count++ ;
}
second. next = second. next. next;
return node. next;
}
public static ListNode removeNthFromEnd ( ListNode head, int n) {
ListNode node = new ListNode ( 0 , head) ;
ListNode first = head, second = node;
for ( int i = 0 ; i < n; i++ ) {
first = first. next;
}
while ( first != null ) {
first = first. next;
second = second. next;
}
second. next = second. next. next;
return node. next;
}
9、 链表相交(面试题 02.07)
public ListNode getIntersectionNode ( ListNode headA, ListNode headB) {
ListNode node1 = headA;
ListNode node2 = headB;
while ( node1 != node2) {
node1 = ( node1 == null ) ? headB : node1. next;
node2 = ( node2 == null ) ? headA : node2. next;
}
return node1;
}
10、环形链表 II(力扣142)
public ListNode detectCycle ( ListNode head) {
ListNode fast = head, slow = head;
while ( true ) {
if ( fast == null || fast. next == null ) {
return null ;
}
fast = fast. next. next;
slow = slow. next;
if ( slow == fast) {
break ;
}
}
fast = head;
while ( fast != slow) {
fast = fast. next;
slow = slow. next;
}
return fast;