Swap Nodes in Pairs

思路:需要构造一个头指针,指向链表。一次比较两个指针,将其翻转,临界条件是pre != null(链表长度为偶数) && pre.next != null(链表长度为奇数),然后更新头指针和pre

public ListNode swapPairs(ListNode head) {
if(head == null) return null;
ListNode newNode = new ListNode(0);
ListNode node = newNode;
node.next = head;
ListNode ptr = head;
while(ptr != null && ptr.next != null){
node.next = ptr.next;
ptr.next = ptr.next.next;
node.next.next = ptr;
node = node.next.next;
ptr = ptr.next;
}
return newNode.next;
}

Insertion Sort List

思路:构造一个头指针node,指向结果链表。每次取出head和结果链表中的节点val进行比较,知道找到应该插入的位置,插入,然后重置node和head

public ListNode insertionSortList(ListNode head) {
if(head == null) return null;
ListNode node = new ListNode(0);
while(head != null){
ListNode pre = node;
while(pre.next != null && pre.next.val <= head.val){
pre = pre.next;
}
ListNode temp = head.next;
head.next = pre.next;
pre.next = head;
head = temp;
}
return node.next;
}

Odd Even Linked List

思路:依次取出奇数位节点和偶数位节点,然后将奇数尾和偶数头连接起来

public ListNode oddEvenList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode odd = head;
ListNode even = head.next;
ListNode temp = even;
while(even != null && even.next != null){
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = temp;
return head;
}

Remove Duplicates from Sorted List II

思路:构造一个头结点指向链表,后面利用双指针判断val是否一致,若不一致则三个指针同时后移,若一直则尾指针后移直到不一致,利用头指针跳过所有重复的节点。

public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode ptr = new ListNode(0);
ptr.next = head;
ListNode copy = ptr;
ListNode pre = head;
ListNode pos = head.next;
while(pos != null){
if(pos.val != pre.val){
ptr = ptr.next;
pre = pre.next;
pos = pos.next;
}
else{
while(pos != null && pos.val == pre.val){
pos = pos.next;
}
ptr.next = pos;
if(pos != null){
pre = pos;
pos = pos.next;
}
}
}
//ptr.next = null;
return copy.next;
}

Merge k Sorted Lists

思路:利用归并的思想,依次将链表的列表从中间分开,然后依次合并两个已排好序的链表

public ListNode mergeKLists(ListNode[] lists) {
int len = lists.length;
if(len == 0) return null;
return helper(lists,0,len - 1);
} public ListNode helper(ListNode[] lists,int l,int r){
if(l < r){
int m = l + (r - l) / 2;
return merge(helper(lists,l,m),helper(lists,m + 1,r));
}
return lists[l];
} public ListNode merge(ListNode l1,ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = merge(l1.next,l2);
return l1;
}
else{
l2.next = merge(l1,l2.next);
return l2;
}
}

类似的:Sort List,也是归并的思想

Reverse Nodes in k-Group

思路:构造一个头指针指向链表,依次往后当长度等于k时,则翻转链表,这个过程注意需要保存要翻转链表的头结点

public ListNode reverseKGroup(ListNode head, int k) {
ListNode ptr = new ListNode(0);
ptr.next = head;
ListNode ptr1 = ptr;
ListNode curNode = head;
int cnt = 0;
while(curNode != null){
ListNode left = head;
cnt++;
if(cnt == k){
ListNode tail = curNode.next;
curNode.next = null;
ListNode leftCopy = left;
reverse(ptr1,left,curNode);
leftCopy.next = tail;
curNode = tail;
head = curNode;
ptr1 = leftCopy;
cnt = 0;
continue;
}
curNode = curNode.next;
}
return ptr.next;
} public void reverse(ListNode ptr1,ListNode left,ListNode right){
ListNode p = null;
ListNode q = left;
while(left != right){
left = left.next;
q.next = p;
p = q;
q = left;
}
left.next = p;
ptr1.next = left;
}

141 双指针,一个快,一个慢,若过程中两指针相同则有环

19/61 双指针,相隔n

21 递归

160 得到两个链表的长度,重置长链表起点使之和短链表一致直到同时达到连接点

83/203 双指针

206 翻转指针

92 找到要翻转指针的范围,翻转

2/445 首先判断两链表长度,将长的那个作为被加数,即最终一定是加数先置为null,然后一位位相加,注意进位,然后判断是否多余的位上val都是9的情况,最后判断是否需要增加尾指针的情况;445比2多了一个翻转操作

86 构造两个头结点,分别表示不小于x的链表和小于x的链表,依次构造完以后拼接起来

常用的小模块和方法:

  1. 翻转链表
  2. 快慢指针得到链表中点
  3. 构造一个节点指向头结点

练习:

Palindrome Linked List:找到中点、翻转指针、依次比较

Delete Node in a Linked List:只能从要删除节点入手,则依次用后节点的val覆盖前节点的val

Reorder List:找到中点、翻转指针、依次连接

Linked List Cycle II:快慢指针找到相遇的位置,然后一个从head位置走,一个从相遇位置走,在环节点处相遇(注意没环的情况)

Convert Sorted List to Binary Search Tree:快慢指针找中点即头结点,递归建树

链表需注意:判头判尾是否为空、写到.next .val判断当前指针是否为空

LeetCode----Linked List的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  2. LeetCode & linked list bug

    LeetCode & linked list bug add-two-numbers shit test /** * Definition for singly-linked list. * ...

  3. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. LeetCode——Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  8. LeetCode——Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  9. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  10. [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

随机推荐

  1. Android 图片浏览器 从原来位置放大至全屏显示

    android 图片浏览器 特点: 1.从网络加载图片,只需要传图片地址数组即可 2.点击图片,从原来位置放大至全屏 3.支持手势操作 4.完全自定义布局 项目源码请到GitHub下载:https:/ ...

  2. 关于iOS和OS X废弃的API你需要知道的一切

    如你所知,已废弃(Deprecated)的API指的是那些已经过时的并且在将来某个时间最终会被移除掉的方法或类.通常,苹果在引入一个更优秀的API后就会把原来的API给废弃掉.因为,新引入的API通常 ...

  3. (转) 浅析HTML5在移动应用开发中的使用

    (转)浅析HTML5在移动应用开发中的使用 (原)http://www.iteye.com/magazines/67   2012-03-07  来自 UECD.163.com  编辑 wangguo ...

  4. SQL GROUP BY 中的TOP N

    一个示例表test(select * from test): id gid    age    username1 1      11     zhangsan2 1      13     zhan ...

  5. GFW-新闭关锁国政策

    当八国联军的炮火轰开古老的北京城门,不知道腐朽的清政府是否依旧认为闭关锁国政策是一项正确的国策?清政府实施闭关锁国政策基于2点考虑:1.安全,国家安全,不想让这些洋鬼子来捣乱.2.我天朝大国,根本无需 ...

  6. 用递归法判断字符串A中包含多少个字符串B

    string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次. 为此想了一个算法. public static void CountIndex ...

  7. 深入理解css系列:清除浮动

    如果出现div嵌套,内层元素浮动,而外层包裹的父元素div未设置高度的时候,那么会出现外层不能被撑开的情况. HTML标签代码: <div class="wrap"> ...

  8. CTO干点啥?

    1.负责技术 2.负责人才 3.负责业务(需求) 4.负责组织

  9. 字符串反转C#的实现

    字符串反转是面试过程中出现频率较高的算法题,今天一个牛同事让我用C#帮他实现这个算法,前提当然是不能使用类库. 例如: how are you 的反转结果为 you are how. 算法1: 是我当 ...

  10. Photoshop的评价

    Photoshop是Adobe公司旗下最为出名的图像处理软件之一. Photoshop的功能性:主要处理以像素所构成的数字图象.使用其众多的编修与绘图工具,可以有效地进行图片编辑工作.支持Window ...