1、Swap Nodes in Pairs
Total Accepted: 9085 Total Submissions: 28748 My Submissions
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
2、Reverse Nodes in k-Group
Total Accepted: 5628 Total Submissions: 23416 My Submissions
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
解题思路,本质上,1和2是一样的,1是2的特殊情况,当k = 2时,2就变为1了。
关于2,我的思路是这样的。new一个ListNode,同时声明一个指针指向该链表。扫描链表,发现长度等于k,就逆转。如果长度不足k,直接返回其本身。
这样速度还可以,Java 300多ms。然后1调用2的方法,将k改为2即可。
关于1,其实还有一种思路,就是借助额外空间。将链表所有数据存入list中,然后建立链表,速度不是很快。
Total Accepted: 9085 Total Submissions: 28748 My Submissions
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
2、Reverse Nodes in k-Group
Total Accepted: 5628 Total Submissions: 23416 My Submissions
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
解题思路,本质上,1和2是一样的,1是2的特殊情况,当k = 2时,2就变为1了。
关于2,我的思路是这样的。new一个ListNode,同时声明一个指针指向该链表。扫描链表,发现长度等于k,就逆转。如果长度不足k,直接返回其本身。
这样速度还可以,Java 300多ms。然后1调用2的方法,将k改为2即可。
关于1,其实还有一种思路,就是借助额外空间。将链表所有数据存入list中,然后建立链表,速度不是很快。
1、Java AC list思路
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null){
return head;
}
List<Integer> list = new ArrayList<Integer>();
ListNode p = head;
int len = 0;
while(p != null){
list.add(p.val);
p = p.next;
len++;
}
ListNode node = new ListNode(0);
p = node;
int k = 1;
while(k < len){
p.next = new ListNode(list.get(k));
p = p.next;
p.next = new ListNode(list.get(k-1));
p = p.next;
k += 2;
}
if (len % 2 != 0) {
p.next = new ListNode(list.get(k-1));
}
return node.next;
}
}
1、Java AC 借助2的思路,将k改为2
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode curNode;
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
return reverseKGroup(head, 2);
}
public ListNode reverseKGroup(ListNode head, int k) {
curNode = head;
ListNode node = new ListNode(0);
ListNode p = node;
boolean flag = true;
while(curNode != null){
ListNode kGroup = reverseK(k);
if(flag){
flag = false;
p.next = kGroup;
}else{
int count = 0;
while(count < k){
p = p.next;
count++;
}
p.next = kGroup;
}
}
return node.next;
}
public ListNode reverseK(int k){
ListNode node = new ListNode(0);
node.next = curNode;
ListNode p = curNode;
int count = 0;
while(p != null){
count++;
p = p.next;
if(count >= k){
break;
}
}
if(p == null && count < k){
curNode = null;
return node.next;
}
count = 0;
ListNode q = null;
ListNode end = null;
while(count < k){
q = curNode;
curNode = curNode.next;
q.next = end;
end = q;
count++;
}
return end;
}
}
2、Java AC
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode curNode;
public ListNode reverseKGroup(ListNode head, int k) {
curNode = head;
ListNode node = new ListNode(0);
ListNode p = node;
boolean flag = true;
while(curNode != null){
ListNode kGroup = reverseK(k);
if(flag){
flag = false;
p.next = kGroup;
}else{
int count = 0;
while(count < k){
p = p.next;
count++;
}
p.next = kGroup;
}
}
return node.next;
}
public ListNode reverseK(int k){
ListNode node = new ListNode(0);
node.next = curNode;
ListNode p = curNode;
int count = 0;
while(p != null){
count++;
p = p.next;
if(count >= k){
break;
}
}
if(p == null && count < k){
curNode = null;
return node.next;
}
count = 0;
ListNode q = null;
ListNode end = null;
while(count < k){
q = curNode;
curNode = curNode.next;
q.next = end;
end = q;
count++;
}
return end;
}
}