public ListNode RemoveNthFromEnd2(ListNode head, int n) { var s = new Stack<ListNode>(); var t = head; if (t != null) { while (t != null) { s.Push(t); t = t.next; } for (var i = 1; i <= n; i++) { t = s.Pop(); } if (t == head) { return head.next; } if (t.next == null) { t = s.Pop(); t.next = null; return head; } t.val = t.next.val; t.next = t.next.next; return head; } return head; }
public bool IsPalindrome2(ListNode head) { if (head == null || head.next == null) { return true; } var stack = new Stack<ListNode>(); var tmp = head; var k = 1; while (tmp != null) { stack.Push(tmp); tmp = tmp.next; k++; } var h= head; for (var i = 1; i <= k/2; i++) { var t = stack.Pop(); if (h.val != t.val) { return false; } h = h.next; } return true; }
public ListNode RemoveNthFromEnd(ListNode head, int n) { var l = new List<ListNode>(); var t = head; while (t != null) { l.Add(t); t = t.next; } t = head; for (var i = 1; i < l.Count - n; i++) { t.next = l[i]; t = t.next; } for (var i = l.Count - n + 1; i < l.Count; i++) { t.next = l[i]; t = t.next; } t.next = null; return n==l.Count?head.next:head; }
public ListNode GetIntersectionNode3(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; } var a = headA; var b = headB; while (a != b) { a = a == null ? headB : a.next; b = b == null ? headA : b.next; } return a; }
public bool IsPalindrome(ListNode head) { if (head == null || head.next == null) { return true; } var h = head; var t = h.next; while (t.next != null) { h = t; t = t.next; } h.next = null; return head.val == t.val && IsPalindrome(head.next); }
public static ListNode DeleteDuplicates3(ListNode head) { if (head == null || head.next == null) { return head; } head.next = DeleteDuplicates3(head.next); return head.val == head.next.val ? head.next : head; }
public static ListNode MergeTwoLists2(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; ListNode h = null; var x = l1.val <= l2.val; h = x ? l1 : l2; h.next = x ? MergeTwoLists2(l1.next, l2) : MergeTwoLists2(l1, l2.next); return h; }
public static ListNode ReverseList1(ListNode head) { if (head == null) { return null; } var nodes = new Stack<ListNode>(); var n = head; while (n.next != null) { nodes.Push(n); n = n.next; } head = n; while(nodes.Count != 0) { n.next = nodes.Pop(); n = n.next; } n.next = null; return head; }
public static ListNode ReverseList2(ListNode head) { if (head == null) { return null; } var x = head; var y = head.next; head.next = null; while (y != null) { var tmp = y.next; y.next = x; x = y; y = tmp; } return x; }
public static ListNode RemoveElements(ListNode head, int val) { while (head!=null&&head.val == val) { head = head.next; } if (head == null) { return null; } var h = head; var t = head.next; while (t != null) { if(t.val != val) { t = t.next; h = h.next; continue; } h.next = t.next; t = t.next; } return head; }
public static ListNode RemoveElements2(ListNode head, int val) { if (head == null) { return null; } if (head.val == val) { return RemoveElements2(head.next, val); } head.next = RemoveElements2(head.next, val); return head; }
public static ListNode OddEvenList3(ListNode head) { if (head==null) { return null; } var odd = head; var even = head.next; var evenHead = head.next; while (even != null && even.next != null) { odd.next = odd.next.next; even.next = even.next.next; odd = odd.next; even = even.next; } odd.next = evenHead; return head; }
public static ListNode OddEvenList2(ListNode head) { if (head == null) { return null; } var o = head; var e = head.next; var e1 = e; while (o.next != null && o.next.next != null) { o.next = o.next.next; o = o.next; if(e.next != null && e.next.next != null) { e.next = e.next.next; e = e.next; } } o.next = e1; if (e != null) { e.next = null; } return head; }
public static ListNode OddEvenList1(ListNode head) { if (head == null || head.next == null) { return head; } var e = head; var o = head.next; var o1 = head.next; while (e.next!=null&&e.next.next!=null&&o.next!=null&&o.next.next!=null) { e.next = e.next.next; o.next = o.next.next; e = e.next; o = o.next; } if (o.next != null) { e.next = o.next; e = e.next; o.next = null; } e.next = o1; return head; }
public static ListNode DeleteDuplicates1(ListNode head) { if (head == null) { return null; } var h = head; while (h != null) { var n = h.next; while (n != null && n.val == h.val) { n = n.next; } h.next = n; h = h.next; } return head; }
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { var nodeA = headA; var nodeB = headB; while (nodeA != null) { while (nodeB != null) { if (nodeB == nodeA) { break; } nodeB = nodeB.next; } if (nodeB == nodeA) { break; } nodeA = nodeA.next; } return nodeA; }
public static ListNode DeleteDuplicates2(ListNode head) { if (head == null) { return null; } var p = head; var q = head.next; while(q!=null) { if (p.val!=q.val) { p.next = q; p = p.next; } q= q.next; } p.next = null; return head; }
// The original structure is changed. public ListNode GetIntersectionNode2(ListNode headA, ListNode headB) { var listA = new Stack<ListNode>(); var listB = new Stack<ListNode>(); while (headA != null) { listA.Push(headA); headA = headA.next; } ListNode newHeadA = null; if (listA.Count != 0) { newHeadA = listA.Pop(); var h = newHeadA; while (listA.Count != 0) { h.next = listA.Pop(); h = h.next; } h.next = null; } while (headB != null) { listA.Push(headB); headB = headB.next; } ListNode newHeadB = null; if (listB.Count != 0) { newHeadB = listA.Pop(); var h = newHeadB; while (listB.Count != 0) { h.next = listB.Pop(); h = h.next; } h.next = null; } ListNode intersection = null; while (newHeadA == newHeadB && newHeadA!=null && newHeadB!=null) { intersection = newHeadA; newHeadA = newHeadA.next; newHeadB = newHeadB.next; } return intersection; }
public static void DeleteNode(ListNode node) { ListNode next = node.next; node.val = next.val; node.next = next.next; }
public static ListNode MergeTwoLists1(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } ListNode h = null; if (l1.val <= l2.val) { h = l1; l1 = l1.next; } else { h = l2; l2 = l2.next; } ListNode m = h; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { m.next = l1; l1 = l1.next; } else { m.next = l2; l2 = l2.next; } m = m.next; } var l3 = l1 == null ? l2 : l1; while (l3 != null) { m.next = l3; l3 = l3.next; m = m.next; } m.next = null; return h; }