常规题
class Solution {
public ListNode reverseList ( ListNode head) {
if ( head == null ) {
return null ;
}
ListNode pre = null ;
ListNode cur = head;
while ( cur != null ) {
ListNode next = cur. next;
cur. next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
class Solution {
public ListNode reverseBetween ( ListNode head, int left, int right) {
ListNode dummyNode = new ListNode ( - 1 ) ;
dummyNode. next = head;
ListNode pre = dummyNode;
for ( int i = 0 ; i < left - 1 ; i++ ) {
pre = pre. next;
}
ListNode cur = pre. next;
for ( int i = 0 ; i < right - left; i++ ) {
ListNode next = cur. next;
cur. next = next. next;
next. next = pre. next;
pre. next = next;
}
return dummyNode. next;
}
}
class Solution {
public ListNode partition ( ListNode head, int x) {
ListNode smallHead = new ListNode ( - 1 ) ;
ListNode small = smallHead;
ListNode largeHead = new ListNode ( - 1 ) ;
ListNode large = largeHead;
while ( head!= null ) {
if ( head. val < x) {
small. next = head;
small = small. next;
} else {
large. next = head;
large = large. next;
}
head = head. next;
}
small. next = largeHead. next;
large. next = null ;
return smallHead. next;
}
}
class Solution {
public ListNode oddEvenList ( ListNode head) {
if ( head == null ) {
return null ;
}
ListNode evenHead = head. next;
ListNode even = evenHead;
ListNode odd = head;
while ( ( even != null && even. next != null ) ) {
odd. next = even. next;
odd = odd. next;
even. next = odd. next;
even = even. next;
}
odd. next = evenHead;
return head;
}
}
class Solution {
public ListNode addTwoNumbers ( ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode ( - 1 ) ;
ListNode cur = dummyHead;
int carry = 0 ;
while ( l1 != null && l2 != null ) {
int sum = l1. val + l2. val + carry;
int nodeVal = sum % 10 ;
carry = sum / 10 ;
cur. next = new ListNode ( nodeVal) ;
cur = cur. next;
l1 = l1. next;
l2 = l2. next;
}
while ( l1 != null ) {
int sum = l1. val + carry;
int nodeVal = sum % 10 ;
carry = sum / 10 ;
cur. next = new ListNode ( nodeVal) ;
cur = cur. next;
l1 = l1. next;
}
while ( l2 != null ) {
int sum = l2. val + carry;
int nodeVal = sum % 10 ;
carry = sum / 10 ;
cur. next = new ListNode ( nodeVal) ;
cur = cur. next;
l2 = l2. next;
}
if ( carry > 0 ) {
cur. next = new ListNode ( carry) ;
}
return dummyHead. next;
}
}
class Solution {
public ListNode addTwoNumbers ( ListNode l1, ListNode l2) {
Stack < Integer > s1 = new Stack < > ( ) ;
Stack < Integer > s2 = new Stack < > ( ) ;
while ( l1 != null ) {
s1. push ( l1. val) ;
l1 = l1. next;
}
while ( l2 != null ) {
s2. push ( l2. val) ;
l2 = l2. next;
}
int carry = 0 ;
ListNode tail = null ;
while ( ! s1. isEmpty ( ) || ! s2. isEmpty ( ) || carry != 0 ) {
int v1 = s1. isEmpty ( ) ? 0 : s1. pop ( ) ;
int v2 = s2. isEmpty ( ) ? 0 : s2. pop ( ) ;
int val = v1 + v2 + carry;
carry = val / 10 ;
val %= 10 ;
ListNode node = new ListNode ( val) ;
node. next = tail;
tail = node;
}
return tail;
}
}
class Solution {
public boolean isPalindrome ( ListNode head) {
if ( head == null ) {
return false ;
}
if ( head. next == null ) {
return true ;
}
List < Integer > vals = new ArrayList < > ( ) ;
ListNode p = head;
while ( p!= null ) {
vals. add ( p. val) ;
p= p. next;
}
int size = vals. size ( ) ;
int middle = size / 2 ;
for ( int i = 0 ; i < middle ; i++ ) {
if ( ! vals. get ( i) . equals ( vals. get ( size - 1 - i) ) ) {
return false ;
}
}
return true ;
}
}
删除节点
class Solution {
public ListNode deleteDuplicates ( ListNode head) {
if ( head == null ) {
return head;
}
ListNode cur = head;
while ( cur. next != null ) {
if ( cur. val == cur. next. val) {
cur. next = cur. next. next;
} else {
cur = cur. next;
}
}
return head;
}
}
class Solution {
public ListNode removeElements ( ListNode head, int val) {
ListNode dummyNode = new ListNode ( - 1 ) ;
dummyNode. next = head;
ListNode cur = dummyNode;
while ( cur. next != null ) {
if ( cur. next. val == val) {
cur. next = cur. next. next;
} else {
cur = cur. next;
}
}
return dummyNode. next;
}
}
class Solution {
public ListNode deleteDuplicates ( ListNode head) {
if ( head == null ) {
return null ;
}
ListNode dummyNode = new ListNode ( - 1 ) ;
dummyNode. next = head;
ListNode cur = dummyNode;
while ( cur. next != null && cur. next. next != null ) {
if ( cur. next. val == cur. next. next. val) {
int x = cur. next. val;
while ( cur. next != null && cur. next. val == x) {
cur. next = cur. next. next;
}
} else {
cur = cur. next;
}
}
return dummyNode. next;
}
}
19. 删除链表的倒数第 N 个结点
class Solution {
public ListNode removeNthFromEnd ( ListNode head, int n) {
if ( head == null ) {
return null ;
}
int len = 0 ;
ListNode p = head;
while ( p != null ) {
len++ ;
p = p. next;
}
int index = len - n;
ListNode dummyHead = new ListNode ( - 1 ) ;
dummyHead. next = head;
int i = 0 ;
ListNode pre = dummyHead;
ListNode cur = head;
while ( i < index) {
pre = cur;
cur = cur. next;
i++ ;
}
pre. next = cur. next;
return dummyHead. next;
}
}
交换旋转
class Solution {
public ListNode swapPairs ( ListNode head) {
ListNode dummyNode = new ListNode ( - 1 ) ;
dummyNode. next = head;
ListNode temp = dummyNode;
while ( temp. next != null && temp. next. next!= null ) {
ListNode node1 = temp. next;
ListNode node2 = temp. next. next;
temp. next = node2;
node1. next = node2. next;
node2. next = node1;
temp = node1;
}
return dummyNode. next;
}
}
class Solution {
public ListNode reverseKGroup ( ListNode head, int k) {
ListNode hair = new ListNode ( 0 ) ;
hair. next = head;
ListNode pre = hair;
while ( head != null ) {
ListNode tail = pre;
for ( int i = 0 ; i < k; ++ i) {
tail = tail. next;
if ( tail == null ) {
return hair. next;
}
}
ListNode nex = tail. next;
ListNode [ ] reverse = myReverse ( head, tail) ;
head = reverse[ 0 ] ;
tail = reverse[ 1 ] ;
pre. next = head;
tail. next = nex;
pre = tail;
head = tail. next;
}
return hair. next;
}
public ListNode [ ] myReverse ( ListNode head, ListNode tail) {
ListNode prev = tail. next;
ListNode p = head;
while ( prev != tail) {
ListNode nex = p. next;
p. next = prev;
prev = p;
p = nex;
}
return new ListNode [ ] { tail, head} ;
}
}
61. 旋转链表
class Solution {
public ListNode rotateRight ( ListNode head, int k) {
if ( head == null || k == 0 ) {
return head;
}
ListNode cur = head;
int len = 0 ;
while ( cur != null ) {
len++ ;
cur = cur. next;
}
int rightLen = k % len;
if ( rightLen == 0 ) {
return head;
}
int leftLen = len - rightLen;
cur = head;
for ( int i = 0 ; i < leftLen - 1 ; i++ ) {
cur = cur. next;
}
ListNode newHead = cur. next;
ListNode p = newHead;
cur. next = null ;
for ( int i = 0 ; i < rightLen - 1 ; i++ ) {
p = p. next;
}
p. next = head;
return newHead;
}
}
链表排序
class Solution {
public ListNode insertionSortList ( ListNode head) {
if ( head == null ) {
return head;
}
ListNode dummyHead = new ListNode ( 0 ) ;
dummyHead. next = head;
ListNode lastSorted = head, curr = head. next;
while ( curr != null ) {
if ( lastSorted. val <= curr. val) {
lastSorted = lastSorted. next;
} else {
ListNode prev = dummyHead;
while ( prev. next. val <= curr. val) {
prev = prev. next;
}
lastSorted. next = curr. next;
curr. next = prev. next;
prev. next = curr;
}
curr = lastSorted. next;
}
return dummyHead. next;
}
}
class Solution {
public ListNode sortList ( ListNode head) {
return sortList ( head, null ) ;
}
public ListNode sortList ( ListNode head, ListNode tail) {
if ( head == null ) {
return head;
}
if ( head. next == tail) {
head. next = null ;
return head;
}
ListNode slow = head, fast = head;
while ( fast != tail) {
slow = slow. next;
fast = fast. next;
if ( fast != tail) {
fast = fast. next;
}
}
ListNode mid = slow;
ListNode list1 = sortList ( head, mid) ;
ListNode list2 = sortList ( mid, tail) ;
ListNode sorted = merge ( list1, list2) ;
return sorted;
}
public ListNode merge ( ListNode head1, ListNode head2) {
ListNode dummyHead = new ListNode ( 0 ) ;
ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
while ( temp1 != null && temp2 != null ) {
if ( temp1. val <= temp2. val) {
temp. next = temp1;
temp1 = temp1. next;
} else {
temp. next = temp2;
temp2 = temp2. next;
}
temp = temp. next;
}
if ( temp1 != null ) {
temp. next = temp1;
} else if ( temp2 != null ) {
temp. next = temp2;
}
return dummyHead. next;
}
}
lass Solution {
public void reorderList ( ListNode head) {
if ( head == null ) {
return ;
}
List < ListNode > list = new ArrayList < ListNode > ( ) ;
ListNode node = head;
while ( node != null ) {
list. add ( node) ;
node = node. next;
}
int i = 0 , j = list. size ( ) - 1 ;
while ( i < j) {
list. get ( i) . next = list. get ( j) ;
i++ ;
if ( i == j) {
break ;
}
list. get ( j) . next = list. get ( i) ;
j-- ;
}
list. get ( i) . next = null ;
}
}