Swap Nodes in Pairs

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

  1. public ListNode swapPairs(ListNode head) {
  2. if(head == null) return null;
  3. ListNode newNode = new ListNode(0);
  4. ListNode node = newNode;
  5. node.next = head;
  6. ListNode ptr = head;
  7. while(ptr != null && ptr.next != null){
  8. node.next = ptr.next;
  9. ptr.next = ptr.next.next;
  10. node.next.next = ptr;
  11. node = node.next.next;
  12. ptr = ptr.next;
  13. }
  14. return newNode.next;
  15. }

Insertion Sort List

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

  1. public ListNode insertionSortList(ListNode head) {
  2. if(head == null) return null;
  3. ListNode node = new ListNode(0);
  4. while(head != null){
  5. ListNode pre = node;
  6. while(pre.next != null && pre.next.val <= head.val){
  7. pre = pre.next;
  8. }
  9. ListNode temp = head.next;
  10. head.next = pre.next;
  11. pre.next = head;
  12. head = temp;
  13. }
  14. return node.next;
  15. }

Odd Even Linked List

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

  1. public ListNode oddEvenList(ListNode head) {
  2. if(head == null || head.next == null) return head;
  3. ListNode odd = head;
  4. ListNode even = head.next;
  5. ListNode temp = even;
  6. while(even != null && even.next != null){
  7. odd.next = even.next;
  8. odd = odd.next;
  9. even.next = odd.next;
  10. even = even.next;
  11. }
  12. odd.next = temp;
  13. return head;
  14. }

Remove Duplicates from Sorted List II

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

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if(head == null || head.next == null) return head;
  3. ListNode ptr = new ListNode(0);
  4. ptr.next = head;
  5. ListNode copy = ptr;
  6. ListNode pre = head;
  7. ListNode pos = head.next;
  8. while(pos != null){
  9. if(pos.val != pre.val){
  10. ptr = ptr.next;
  11. pre = pre.next;
  12. pos = pos.next;
  13. }
  14. else{
  15. while(pos != null && pos.val == pre.val){
  16. pos = pos.next;
  17. }
  18. ptr.next = pos;
  19. if(pos != null){
  20. pre = pos;
  21. pos = pos.next;
  22. }
  23. }
  24. }
  25. //ptr.next = null;
  26. return copy.next;
  27. }

Merge k Sorted Lists

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

  1. public ListNode mergeKLists(ListNode[] lists) {
  2. int len = lists.length;
  3. if(len == 0) return null;
  4. return helper(lists,0,len - 1);
  5. }
  6. public ListNode helper(ListNode[] lists,int l,int r){
  7. if(l < r){
  8. int m = l + (r - l) / 2;
  9. return merge(helper(lists,l,m),helper(lists,m + 1,r));
  10. }
  11. return lists[l];
  12. }
  13. public ListNode merge(ListNode l1,ListNode l2){
  14. if(l1 == null) return l2;
  15. if(l2 == null) return l1;
  16. if(l1.val < l2.val){
  17. l1.next = merge(l1.next,l2);
  18. return l1;
  19. }
  20. else{
  21. l2.next = merge(l1,l2.next);
  22. return l2;
  23. }
  24. }

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

Reverse Nodes in k-Group

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

  1. public ListNode reverseKGroup(ListNode head, int k) {
  2. ListNode ptr = new ListNode(0);
  3. ptr.next = head;
  4. ListNode ptr1 = ptr;
  5. ListNode curNode = head;
  6. int cnt = 0;
  7. while(curNode != null){
  8. ListNode left = head;
  9. cnt++;
  10. if(cnt == k){
  11. ListNode tail = curNode.next;
  12. curNode.next = null;
  13. ListNode leftCopy = left;
  14. reverse(ptr1,left,curNode);
  15. leftCopy.next = tail;
  16. curNode = tail;
  17. head = curNode;
  18. ptr1 = leftCopy;
  19. cnt = 0;
  20. continue;
  21. }
  22. curNode = curNode.next;
  23. }
  24. return ptr.next;
  25. }
  26. public void reverse(ListNode ptr1,ListNode left,ListNode right){
  27. ListNode p = null;
  28. ListNode q = left;
  29. while(left != right){
  30. left = left.next;
  31. q.next = p;
  32. p = q;
  33. q = left;
  34. }
  35. left.next = p;
  36. ptr1.next = left;
  37. }

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. spring 包下载地址

    留着,以备不时之需: http://repo.spring.io/libs-release-local/org/springframework/spring/

  2. Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  3. 【LeetCode OJ】Symmetric Tree

    Problem Link: https://oj.leetcode.com/problems/symmetric-tree/ To solve the problem, we can traverse ...

  4. Json序列化

    参考文章 C# 解析json的方法 http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-json.html

  5. 安装Python2.7环境

    1.下载并根据提示安装python_2.7.10_x64 2.将安装目录配置到环境变量path中 3.在命令后台中输入Python,出现下图中信息表示安装成功

  6. JavaScript和Java之间的关系

    今天来简单而又详细地说说JavaScript和Java的关系. 开门见山总结性一句话,它们之间的关系 = 雷锋和雷峰塔之间的关系,换句话说:它们之间没什么关系. 但往往有不少初学者甚至中级者认为它们之 ...

  7. 用python+selenium登录cnblog后新增文章后再次删除该文章

    目的:登录cnblog后新增文章后再次删除该文章并验证 代码如下: #coding: utf-8 from selenium import webdriver from time import sle ...

  8. 【JS】点击目标外事件与IFRAM自适应高度

    一.点击目标外事件 $(document).mouseup(function(e){ var _con = $('.dropdown-multiSelect-list'); // 设置目标区域 if( ...

  9. [翻译]Primer on Cognitive Computing(认知计算入门)

    Source Kelly J., Primer on Cognitive Computing 20150216. 侵删,联系方式:zhoujiagen\@gmail.com. 按A candidate ...

  10. php + sqlserver

    Dbconn <?php class DbConn{ private $conn; private $rs; private function __construct(){ $serverNam ...