LeetCode LinkedList

1. 234 Palindrome Lineked List

找中间  反转  比较

 1     public boolean isPalindrome(ListNode head) {
 2         if (head == null)
 3         {
 4             return true;
 5         }
 6         ListNode fast = head;
 7         ListNode slow = head;
 8         while (fast != null && fast.next != null)
 9         {
10             slow = slow.next;
11             fast = fast.next.next;
12         }
13         if (fast != null)
14         {
15             slow = slow.next;
16         }
17         slow = reverse(slow);
18         fast = head;
19         while (slow != null)
20         {
21             if (slow.val != fast.val)
22             {
23                 return false;
24             }
25             slow = slow.next;
26             fast = fast.next;
27         }
28         return true;
29     }
30     public ListNode reverse(ListNode head)
31     {
32         ListNode pre = null;
33         while (head != null)
34         {
35             ListNode next = head.next;
36             head.next = pre;
37             pre = head;
38             head = next;
39         }
40         return pre;
41     }
View Code

 2. 237 Delete Node in a Linked List

后一个数变前一个,连接到下下数

    public void deleteNode(ListNode node) {
      node.val = node.next.val;
      node.next = node.next.next;
    }
View Code

3. 445 Add Two Number II

栈装两个数据并相加,记住创建下一个数据并连前一个数据

 1     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 2         Stack<Integer> s1 = new Stack<Integer>();
 3         Stack<Integer> s2 = new Stack<Integer>();
 4         while (l1 != null)
 5         {
 6             s1.push(l1.val);
 7             l1 = l1.next;
 8         }
 9         while (l2 != null)
10         {
11             s2.push(l2.val);
12             l2 = l2.next;
13         }
14         int sum = 0;
15         ListNode list = new ListNode(0);
16         while ( !s1.isEmpty() || !s2.isEmpty())
17         {
18             if (!s1.isEmpty()) sum += s1.pop();
19             if (!s2.isEmpty()) sum += s2.pop();
20             list.val = sum % 10;
21             ListNode head = new ListNode(sum/10);
22             head.next = list;
23             sum /= 10;
24             list = head;
25         }
26         return list.val==0? list.next:list;
27     }
View Code

 4. 2 Add Two Number

加 head pre

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode pre = head;
        int sum = 0;
        while (l1 != null || l2 != null)
        {
            if (l1 != null)
            {
               sum += l1.val;
               l1= l1.next;
            }
            if (l2 != null)
            {
               sum += l2.val;
               l2= l2.next;
            }
            ListNode node = new ListNode(sum % 10);
            pre.next = node;
            sum /= 10;
            pre = pre.next;
        }
        if (sum != 0)
        {
            ListNode node = new ListNode(sum % 10);
            pre.next = node;
        }
        return head.next;
    }
View Code

5. 19 Remove Nth Node From End of List

加start  fast 走n+1 步, 慢的就到n的头一个


    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode start = new ListNode(0);
        ListNode fast = start;
        ListNode slow = start;
        start.next = head;
        for (int i = 1; i <= n + 1; i++)
        {
            fast = fast.next;
        }
        while (fast != null)
        {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return start.next;
    }
View Code

6. 21 Merge Two Sorted Lists

减而治之  递归

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null)
        {
            return l2;
        }
        if (l2 == null)
        {
            return l1;
        }
        ListNode merge;
        if (l1.val < l2.val)
        {
            merge = l1;
            merge.next = mergeTwoLists(l1.next, l2);
        }else
        {
            merge = l2;
            merge.next = mergeTwoLists(l1,l2.next);
        }
        return merge;
    }
View Code

 7. 25 Reverse Nodes in k-Group    错

减而治之 。 递归k 个 k = 0

public ListNode reverseKGroup(ListNode head, int k) {
    ListNode curr = head;
    int count = 0;
    while (curr != null && count != k) { // find the k+1 node
        curr = curr.next;
        count++;
    }
    if (count == k) { // if k+1 node is found
        curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
        // head - head-pointer to direct part, 
        // curr - head-pointer to reversed part;
        while (count-- > 0) { // reverse current k-group: 
            ListNode tmp = head.next; // tmp - next head in direct part
            head.next = curr; // preappending "direct" head to the reversed list 
            curr = head; // move head of reversed part to a new node
            head = tmp; // move "direct" head to the next node in direct part
        }
        head = curr;
    }
    return head;
}
View Code

找前,找尾,删除,插入 c = 0

    public ListNode reverseKGroup(ListNode head, int k) {
     if (head == null || head.next == null || k < 2)
     {
         return head;
     }
     ListNode helper = new ListNode(0);
     helper.next = head;
     ListNode pre = helper, tail = helper, item;
     int count = 0;
     while (true)
     {
         count = k;
         while (tail != null && count > 0)
         {
             tail = tail.next;
             count --;
         }
         if (tail == null)
         {
             break;
         }
         head = pre.next;
         while (pre.next != tail)
         {
             item = pre.next;
             pre.next = item.next;
             item.next = tail.next;
             tail.next = item;
         }
         pre = head;
         tail = head;
     }
     return helper.next;
    }
View Code

8. 83 Remove Dupliccates from Sorted List c = k;

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        head.next = deleteDuplicates(head.next);
        return head = head.val == head.next.val ? head.next:head;
    }
View Code
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode list = head;
        while (list.next != null)
        {
            if (list.val == list.next.val)
            {
                list = list.next.next;
            }else
            {
                list = list.next;
            }
        }
        return head;
    }
View Code

 4月1号

9. 61 Rotate List    得到长l  移到 l - n %l   旋转

    public ListNode rotateRight(ListNode head, int n) {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode helper = new ListNode(0);
        helper.next = head;
        ListNode fast = helper, slow = helper;
        int i = 0;
        for (i = 0; fast.next != null; i++)
        {
            fast = fast.next;
        }
        for (int j = i - n % i; j > 0; j--)
        {
            slow = slow.next;
        }
        fast.next = helper.next;
        helper.next = slow.next;
        slow.next = null;
        return helper.next;
    }
View Code

10. 82 Remove Duplicates from Sorted List   重复删

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode helper = new ListNode(0);
        helper.next = head;
        ListNode pre = helper, cur = head;
        while (cur != null)
        {
            while (cur.next != null && cur.val == cur.next.val)
            {
                cur = cur.next;
            }
            if (pre.next == cur)
            {
                pre = pre.next;
            }else
            {
                pre.next = cur.next;
            }
            cur = cur.next;
        }
        return helper.next;
    }
View Code
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null)
        {
            return head;
        }
        if (head.next != null && head.val == head.next.val)
        {
            while (head.next != null && head.val == head.next.val)
            {
                head = head.next;
            }
            return deleteDuplicates(head.next);
        }
        else
        {
            head.next = deleteDuplicates(head.next);
        }
        return head;
    }
View Code

11. 23 Merge k Sorted Lists   bugfree

分而治之

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

12. 24 Swap Nodes in Pairs  递归  bugfree

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode item = head.next;
        head.next = swapPairs(item.next);
        item.next = head;
        return item;
    }
View Code

13 86 Partition  List 两个链表, bugfree

    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(0), dummy2 = new ListNode(0);
        ListNode cur1 = dummy1, cur2 = dummy2;
        while (head != null)
        {
            if (head.val < x)
            {
                cur1.next = head;
                cur1 = head;
            }
            else
            {
                cur2.next = head;
                cur2 = head;
            }
            head = head.next;
        }
        cur1.next = dummy2.next;
        cur2.next = null;
        return dummy1.next;
    }
View Code

14 92 Reverse Linked List II     虚点  pre  start then; 错 找到m-1点  删除插入

    public ListNode reverseBetween(ListNode head, int m, int n) {
    if (head == null)
    {
        return head;
    }
    ListNode helper = new ListNode(0);
    ListNode pre = helper;
    pre.next = head;
    for (int i = 0; i < m - 1; i++)
    {
        pre = pre.next;
    }
    ListNode start = pre.next;
    ListNode then = start.next;
    for (int i = 0; i < n - m; i++)
    {
        start.next = then.next;
        then.next = pre.next;
        pre.next = then;
        then = start.next;
    }
    return helper.next;
    }
View Code

 4月2号

15 Convert Sorted Array to Binary Search Tree  先找到长度 中序遍历节点, bugfree

    public TreeNode sortedListToBST(ListNode head) {
        if (head == null)
        {
            return null;
        }
        ListNode cur = head;
        int count = 0;
        while (cur != null)
        {
            cur = cur.next;
            count ++;
        }
        ArrayList<ListNode> list = new ArrayList<ListNode>();
        list.add(head);
        return helper(list, 0, count - 1);
    }
    public TreeNode helper(ArrayList<ListNode> list, int l, int r)
    {
        if (l > r)
        {
            return null;
        }
        int m = l + (r - l) / 2;
        TreeNode left = helper(list, l, m - 1);
        ListNode head = list.get(0);
        TreeNode root = new TreeNode(head.val);
        list.set(0, head.next);
        root.left = left;
        root.right = helper(list, m + 1, r);
        return root;
    }
View Code

16 Copy List with Random Pointer     复制到后面 bugfree

    public RandomListNode copyRandomList(RandomListNode head) {
        if (head == null)
        {
            return null;
        }
        RandomListNode node = head;
        while (node != null)
        {
            RandomListNode next = node.next;
            RandomListNode newNode = new RandomListNode(node.label);
            node.next = newNode;
            newNode.next = next;
            node = next;
        }
        node = head;
        while (node != null)
        {
            if (node.random != null)
            {
                node.next.random = node.random.next;
            }
            node = node.next.next;
        }
        node = head;
        RandomListNode newHead = head.next;
        while (node != null)
        {
            RandomListNode newNode = node.next;
            node.next = newNode.next;
            node = node.next;
            if (node != null)
            {
                newNode.next = node.next;
                newNode = newNode.next;
            }
        }
        return newHead;
    }
View Code

17 Linked List Cycle    快慢指针  检测块 和块下  bugfree

    public boolean hasCycle(ListNode head) {
        if (head == null)
        {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null)
        {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow)
            {
                return true;
            }
        }
        return false;
    }
View Code

 18 Linked List Cycle II 找到环的位置  快慢  慢到头和慢比较

    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null)
        {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow)
            {
                ListNode slow1 = head;
                while (slow1 != slow)
                {
                    slow = slow.next;
                    slow1 = slow1.next;
                }
                return slow;
            }
        }
        return null;
    }
View Code

19  Reorder List    中间 后半反转 合并  bugfree

  public void reorderList(ListNode head) {
        if (head == null || head.next == null)
        {
            return;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null)
        {
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode head1 = head;
        ListNode head2 = slow.next;
        slow.next = null;
        head2 = reverse(head2);
        while (head1 != null && head2 != null)
        {
            ListNode item = head2.next;
            head2.next = head1.next;
            head1.next = head2;
            head2 = item;
            head1 = head1.next.next;
        }
    }
private ListNode reverse(ListNode head)   
{  
    ListNode pre = null;
    while (head != null)
    {
        ListNode node = head.next;
        head.next = pre;
        pre = head;
        head = node;
    }
    return pre;
} 
View Code

20.Insertion Sort List  每次排序回归跟  小于 错  开始helper.next 不和head 连

   public ListNode insertionSortList(ListNode head) {
        if (head == null) return head;
  ListNode helper = new ListNode(0); //new starter of the sorted list
        ListNode cur = head; //the node will be inserted
        ListNode pre = helper; //insert node between pre and pre.next
        ListNode next = null; //the next node will be inserted
        //not the end of input list
        while( cur != null ){
            next = cur.next;
            //find the right place to insert
            while( pre.next != null && pre.next.val < cur.val ){
                pre = pre.next;
            }
            //insert between pre and pre.next
            cur.next = pre.next;
            pre.next = cur;
            pre = helper;
            cur = next;
        }
        
        return helper.next;
    }
View Code

21. Sort List  分两半 归并  bugfree

    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null)
        {
            return head;
        }
        ListNode fast = head, slow = head, pre = null;
        while (fast != null && fast.next != null)
        {
            pre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pre.next = null;
        return merge(sortList(head), sortList(slow));
    }
    public ListNode merge(ListNode l1, ListNode l2)
    {
        ListNode helper = new ListNode(0);
        ListNode pre = helper;
        while (l1 != null && l2 != null)
        {
            if (l1.val < l2.val)
            {
                pre.next = l1;
                l1 = l1.next;
            }
            else
            {
                pre.next = l2;
                l2 = l2.next;
            }
            pre = pre.next;
        }
        if (l1 != null)
        {
            pre.next = l1;
        }
        if (l2 != null)
        {
            pre.next = l2;
        }
        return helper.next;
    }
View Code

22. InterSection of Two Linked     a, b 俩个遍历

        if (headA == null || headB == null)
        {
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;
        while (a != b)
        {
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }
View Code

23 203 Remove Linked List Element   递归 同反否下一个

    public ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next:head;
    }
View Code

24  206 Reverse  Linked List 

    public ListNode reverseList(ListNode head) {
                if(head==null || head.next==null)
            return head;
        ListNode nextNode=head.next;
        ListNode newHead=reverseList(nextNode);
        nextNode.next=head;
        head.next=null;
        return newHead;
    }
View Code

 

posted on 2017-03-29 14:30  wheleetcode  阅读(92)  评论(0)    收藏  举报