Linked List
文章目录
leetcode21. Merge Two Sorted Lists
题目链接
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
思路: 找到两个链表的头结点中值较小的为新链表的头结点,然后比较这俩链表的新头结点值,谁小谁就接在新链表后面,然后继续比较下一个节点即可。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null){
return l1 == null ? l2 : l1;
}
ListNode head = (l1.val < l2.val ? l1 : l2);
if(head == l1){
l1 = l1.next;
}else{
l2 = l2.next;
}
head.next = null;
ListNode p = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
p.next = null;
}
p.next = (l1 == null ? l2 : l1);
return head;
}
}
别人思路: 思路基本一样,代码上有些优化,值得借鉴!
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
head.next = null;
ListNode p = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
p.next = null;
}
p.next = (l1 == null ? l2 : l1);
return head.next;
}
}
leetcode2. Add Two Numbers
题目链接
题目:给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = l1;
ListNode pre = head;
//进位标志位
int mark = 0;
int num;
while(l1 != null && l2 != null){
num = l1.val + l2.val + mark;
if(num > 9){
mark = 1;
}else{
mark = 0;
}
l1.val = num % 10;
pre = l1;
l1 = l1.next;
l2 = l2.next;
}
if(l2 != null){
pre.next = l2;
}
l1 = pre.next;
while(l1 != null){
num = l1.val + mark;
if(num > 9){
mark = 1;
}else{
mark = 0;
}
l1.val = num % 10;
pre = l1;
l1 = l1.next;
}
if(mark == 1){
ListNode end = new ListNode(1);
pre.next = end;
}
return head;
}
}
别人的代码: 思路大致相同,只是代码上差距很大。感觉自己的代码十分啰嗦,别人的代码像艺术,自己的代码像shit!
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
head.next = null;
ListNode cur = head;
//进位标志位
int mark = 0;
int x, y;
int digit;
while(l1 != null || l2 != null){
x = (l1 != null ? l1.val : 0);
y = (l2 != null ? l2.val : 0);
//求和
digit = x + y + mark;
mark = digit / 10;
cur.next = new ListNode(digit % 10);
cur = cur.next;
cur.next = null;
if(l1 != null){
l1 = l1.next;
}
if(l2 != null){
l2 = l2.next;
}
}
if(mark > 0){
cur.next = new ListNode(1);
cur = cur.next;
cur.next = null;
}
return head.next;
}
}
leetcode24. Swap Nodes in Pairs
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = null;
ListNode pre = dummy;
ListNode p = head;
ListNode q;
ListNode r;
while(p != null && p.next != null){
q = p.next;
r = q.next;
pre.next = q;
q.next = p;
p.next = null;
pre = p;
p = r;
}
if(p != null){
pre.next = p;
}
return dummy.next;
}
}
leetcode23. Merge k Sorted Lists
题目:合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
题目描述
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
List<ListNode> list = new ArrayList<ListNode>();
for(ListNode l : lists){
list.add(l);
}
if(list.isEmpty()){
return null;
}
int end = list.size() - 1;
while(end > 0){
int begin = 0;
while(begin < end){
list.set(begin, mergeTwoLists(list.get(begin), list.get(end)));
end--;
begin++;
}
}
return list.get(0);
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
head.next = null;
ListNode p = head;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
p.next = l1;
l1 = l1.next;
}else{
p.next = l2;
l2 = l2.next;
}
p = p.next;
p.next = null;
}
p.next = (l1 == null ? l2 : l1);
return head.next;
}
}
leetcode138. Copy List with Random Pointer
题目链接
题目:给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。
返回一个深拷贝的链表。
别人思路: 第一步,将原链表从头遍历,然后将每个节点深拷贝一份(这里的深拷贝意思:创建一个新的节点,里面的值用原节点的值一样),然后将这个新的节点放入哈希表中,其中key为原节点,value为新的节点;
第二步,从头再遍历原链表,每次遍历从哈希表中取以当前节点为key的新节点,获得新的节点之后,新的节点的next为当前节点的next为key的新节点,新的节点的random也是如此。
/**
* Definition for singly-linked list with a random pointer.
* class RandomListNode {
* int label;
* RandomListNode next, random;
* RandomListNode(int x) { this.label = x; }
* };
*/
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
Map<RandomListNode, RandomListNode> map = new HashMap<>();
RandomListNode dummy = new RandomListNode(0);
dummy.next = null;
RandomListNode p = dummy;
RandomListNode q = head;
while(q != null){
p.next = new RandomListNode(q.label);
//以当前的源节点为key,新节点为value
map.put(q, p.next);
p = p.next;
q = q.next;
}
p = dummy;
q = head;
while(q != null){
p.next.random = map.get(q.random);
q = q.next;
p = p.next;
}
return dummy.next;
}
}