解法:
4/17/26About 1 min
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode left = head;
ListNode right = head.next;
ListNode s = head;
int count = 0;
while(s != null){
count++;
s = s.next;
}
if(n == count){
return head.next;
}
while(n < count-1){
right = right.next;
left = left.next;
count--;
}
if(n == 1){
left.next = null;
return head;
}else{
left.next = right.next;
return head;
}
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if(list1 == null){
return list2;
}
if(list2 == null){
return list1;
}
ListNode res = new ListNode();
ListNode move = res;
while(list1 != null || list2 != null){
if(list1 == null){
move.next = list2;
break;
}
else if(list2 == null){
move.next = list1;
break;
}
else if(list1.val <= list2.val){
move.next = list1;
list1 = list1.next;
move = move.next;
}else{
move.next = list2;
list2 = list2.next;
move = move.next;
}
}
return res.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length == 0 || lists == null){
return null;
}
ListNode p = new ListNode();
ListNode res = p;
ArrayList<Integer> list = new ArrayList();
int i = 0;
ListNode s = lists[0];
while(i < lists.length){
while(s == null && (i+1) < lists.length){
s = lists[++i];
}
if(s != null){
list.add(s.val);
s = s.next;
}else{
break;
}
}
Collections.sort(list);
int j = 0;
if(list.size() == 0){
return null;
}
while(j < list.size()){
p.val = list.get(j);
if(j != list.size() - 1){
p.next = new ListNode();
p = p.next;
}
j++;
}
return res;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode temp = head.next;
if(head.next.next != null){
head.next = head.next.next;
}else{
head.next.next = head;
head = head.next;
head.next.next = null;
return head;
}
temp.next = head;
head = temp;
ListNode i = head.next;
while(i.next != null && i.next.next != null){
ListNode t3 = i.next;
ListNode t4 = i.next.next;
ListNode t5 = null;
if(t4.next != null){
t5 = t4.next;
}
i.next = t4;
t4.next = t3;
if(t5 == null){
t3.next = null;
return head;
}
t3.next = t5;
i = i.next.next;
}
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode p = head;
ListNode s = head;
int i;
int[] temp = new int[k];
while(s.next != null){
int count = 0;
for(i = 0;i < k ;i++){
temp[i] = s.val;
if(s.next != null){
s = s.next;
count++;
}
}
if(i == k && (count == k - 1 || count == k)){
for(i = 0;i < k;i++){
p.val = temp[k - i - 1];
p = p.next;
}
}
}
return head;
}
}
解法思路:
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (k == 0 || head == null || head.next == null) {
return head;
}
int n = 1;
ListNode iter = head;
while (iter.next != null) {
iter = iter.next;
n++;
}
int add = n - k % n;
if (add == n) {
return head;
}
iter.next = head;
while (add-- > 0) {
iter = iter.next;
}
ListNode ret = iter.next;
iter.next = null;
return ret;
}
}
解法:判断删除,数组整体移动
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 1) return 1;
int res = nums.length;
int left = nums[0];
int count = 1;
for (int i = 1; i < res; i++) {
if(left == nums[i]) count++;
else {
left = nums[i];
count = 1;
continue;
}
if(count >= 3){
int right = i;
while(right < nums.length && nums[right] == left){
right++;
res--;
}
if(right >= nums.length) return i;
else {
System.arraycopy(nums, right, nums, i, nums.length - right);
i--;
}
}
}
return res;
}
}
解法: 建立新的链表,使用双指针校验,手动添加节点
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode res = new ListNode(0);
ListNode fol = res;
ListNode left = head;
ListNode right = head.next;
while (right != null) {
if (left.val != right.val && left.next == right) {
fol.next = new ListNode(left.val); // add non-duplicate to result list
fol = fol.next;
left = right;
right = right.next;
} else if (left.val != right.val) {
left = right;
right = right.next;
} else {
right = right.next;
}
}
if (left.next == null) {
fol.next = new ListNode(left.val);
}
return res.next;
}
}
解法:
初始时,建立两个指针,分别指向 iter = head 和 next = head.next,判断连个指针所指值的大小
iter.next = null;iter.val == next.val,next 后移iter.val != next.val,将 iter.next 指向 next,将 iter 指向 next,next 后移一位